Applescript Excel 2016 save as PDF

我们两清 提交于 2019-12-02 04:27:06

问题


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

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!!


回答1:


Here's a script for Excel 2016 (version 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



回答2:


Finally I made words, powerpoints and excel works

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


来源:https://stackoverflow.com/questions/37394771/applescript-excel-2016-save-as-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!