Applescript Excel 2016 保存为 PDF [英] Applescript Excel 2016 save as PDF

查看:40
本文介绍了Applescript Excel 2016 保存为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Excel 2016 电子表格保存为 PDF 文件.我有以下从 Objective C 程序中调用的简单 Applescript:

I am trying to save an Excel 2016 spreadsheet as a PDF file. I have following simple Applescript that is called from within an Objective C program:

on saveExcelAsPDF(documentPath, PDFPath)
tell application "Microsoft Excel"
    open file documentPath
    save active sheet in PDFPath as PDF file format
    close active workbook saving no
    quit
end tell
end saveExcelAsPDF

此脚本在使用 Excel 2008 和 2011 时效果很好,但在使用 Excel 2016(版本 15.22)时失败.open 和 save 命令都以不同的方式失败.有人可以帮帮我吗!我花了几个小时试图让它发挥作用.我已经阅读了我能找到的所有关于这个主题的帖子.我什至尝试使用系统事件"来模拟击键.我尝试过的任何东西都不起作用.任何建议将不胜感激.谢谢!!

This script works great using Excel 2008 and 2011, but fails using Excel 2016 (version 15.22). Both the open and save commands fail in different ways. Can someone please help me! I have spent hours trying to get this to work. I have read all the posts on this subject that I can find. I even tried using "System Events" to mimic the keystrokes. Nothing that I have tried works. Any advice will be greatly appreciated. Thanks!!

推荐答案

这是 Excel 2016(版本 15.22) 的脚本.

我在脚本中添加了注释:

I've added comments in the script:

on saveExcelAsPDF(documentPath, PDFPath) -- params = two HFS paths
    set tFile to (POSIX path of documentPath) as POSIX file -- get a posix file object to avoid grant access issue with 'Microsoft Office 2016',  not the same as (file documentPath) when using the 'open  ...' command

    tell application "Microsoft Excel"
        set isRun to running
        set wkbk1 to open workbook workbook file name tFile
        alias PDFPath -- This is necessary to any script for 'Microsoft Office 2016', this avoid errors with any "save ... " command
        save workbook as wkbk1 filename PDFPath file format PDF file format with overwrite
        close wkbk1 saving no
        if not isRun then quit
    end tell
end saveExcelAsPDF

这篇关于Applescript Excel 2016 保存为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆