Applescript Excel 2016 save as PDF

后端 未结 2 1148
北恋
北恋 2021-01-21 10:24

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 saveExcel         


        
相关标签:
2条回答
  • 2021-01-21 10:57

    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
    
    0 讨论(0)
  • 2021-01-21 10:58

    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
    
    0 讨论(0)
提交回复
热议问题