我正在尝试将Excel 2016电子表格另存为PDF文件。我遵循从Objective C程序中调用的简单Applescript:
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命令都以不同的方式失败。有人可以帮帮我吗!我花了好几个小时试图让它工作。我已经阅读了我能找到的关于这个主题的所有帖子。我甚至尝试使用“系统事件”来模仿击键。我没有尝试过任何工作。任何建议将不胜感激。谢谢!
答案 0 :(得分:2)
以下是 Excel 2016(版本15.22)的脚本。
我在剧本中添加了评论:
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
答案 1 :(得分:0)
最后我做了文字,PowerPoint和Excel作品
1. From excel to pdf in batches
on run
set theseFiles to (choose file of type {"com.microsoft.excel.xls", "org.openxmlformats.spreadsheetml.sheet"} ¬
with prompt "Choose the Excel sheets to export to PDF:" with multiple selections allowed)
-- display dialog "theseItems: " & theseItems
repeat with thisFile in theseFiles
tell application "Finder"
set theItemParentPath to container of (thisFile as alias) as text
set theItemName to (name of thisFile) as string
set theItemExtension to (name extension of thisFile)
set theItemExtensionLength to (count theItemExtension) + 1
set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
set theOutputPath to (theOutputPath & ".pdf")
end tell
tell application "Microsoft Excel"
set isRun to running
activate
open thisFile
tell active workbook
alias theOutputPath
-- set overwrite to true
save workbook as filename theOutputPath file format PDF file format with overwrite
--save overwrite yes
close saving no
end tell
-- close active workbook saving no
if not isRun then quit
end tell
end repeat
end run
2. From word to pdf in batches
on run
set theseFiles to (choose file of type {"com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"} with prompt "Choose the Word documents to export to PDF:" with multiple selections allowed)
-- display dialog "theseItems: " & theseItems
repeat with thisFile in theseFiles
tell application "Finder"
set theItemParentPath to container of (thisFile as alias) as text
set theItemName to (name of thisFile) as string
set theItemExtension to (name extension of thisFile)
set theItemExtensionLength to (count theItemExtension) + 1
set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
set theOutputPath to (theOutputPath & ".pdf")
end tell
tell application "Microsoft Word"
--set default file path file path type documents path path theItemParentPath
open thisFile
--delay 1
--set theActiveDocument to active document
--save as theActiveDocument file format format PDF file name theOutputPath
--close theActiveDocument
tell active document
save as file format format PDF file name theOutputPath
close
end tell
end tell
end repeat
end run
3. From ppt to pdf in batches
on run
set theseFiles to (choose file of type {"com.microsoft.powerpoint.ppt", "org.openxmlformats.presentationml.presentation"} with prompt "Choose the PowerPoint Presentations to export to PDF:" with multiple selections allowed)
-- display dialog "theseItems: " & theseItems
repeat with thisFile in theseFiles
tell application "Finder"
set theItemParentPath to container of (thisFile as alias) as text
set theItemName to (name of thisFile) as string
set theItemExtension to (name extension of thisFile)
set theItemExtensionLength to (count theItemExtension) + 1
set theOutputPath to theItemParentPath & (text 1 thru (-1 - theItemExtensionLength) of theItemName)
end tell
tell application "Microsoft PowerPoint"
open thisFile
tell active presentation
save in theOutputPath as save as PDF
close
end tell
end tell
end repeat
end run