Applescript Excel 2016 save as PDF

后端 未结 2 1185
北恋
北恋 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: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
    

提交回复
热议问题