2016-05-23 103 views
2

我試圖將Excel 2016電子表格保存爲PDF文件。我有以下簡單的AppleScript是從客觀的C程序中調用:Applescript Excel 2016保存爲PDF

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版)失敗。打開和保存命令都以不同的方式失敗。有人可以幫幫我嗎!我花了數小時試圖讓這個工作。我已閱讀關於此主題的所有帖子,我可以找到。我甚至嘗試使用「系統事件」來模仿擊鍵。沒有什麼我嘗試過的。任何意見將不勝感激。謝謝!!

+0

什麼是'documentPath'你怎麼調用從ObjC腳本? – vadian

+0

@vadian documentPath和PDFPath都是HFS格式的字符串。 – user1092808

+0

至少嘗試刪除'文件'在開放的行或嘗試'打開(documentPath作爲別名)' – vadian

回答

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 
+0

WOW !!!這個工程很棒!非常感謝,你是怎麼弄出來的?!?!?再次,謝謝!! – user1092808

+0

As我自己的腳本在所有情況下都不起作用,授予訪問權限以打開不在最近文檔列表中的文件,當目標是新文件或目標位於任何'save ...'命令時發出另一個文件夾 我不得不找到一個解決方案,所以我測試了一個簡單的腳本並對其進行了修改,直到它無誤地運行。 – jackjr300