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

查看:137
本文介绍了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)上运行失败.打开和保存命令均以不同的方式失败.有人可以帮帮我吗!我已经花了几个小时试图使它起作用.我已经阅读了有关该主题的所有帖子.我什至尝试使用系统事件"来模拟击键.我没有尝试过任何东西.任何建议将不胜感激.谢谢!

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天全站免登陆