What is the correct syntax to save as active document in Word using Applescript?

This is where I'm going crazy. I've tried countless permutations / variations of "save as active PDF document file format", but none of them work. I am getting AppleScript errors with all of them.

So can someone tell me:

What is the exact syntax for saving the active document as a PDF file in AppleScript using Word?

There seems to be no consistency in the Office for Mac scripts as it works for me for Excel and PowerPoint and even there the syntax is different:

Excel

save active workbook in 'MyFile.pdf' as PDF file format

      

PowerPoint

save active presentation in 'MyFile.pdf' as save as PDF

      

What is the correct syntax for Word?

Thank!

+3


source to share


2 answers


I think I found it eventually:

set myDoc to "/Users/X/Desktop/test.docx"
set pdfSavePath to "Users:X:Desktop:test.pdf"

tell application "Microsoft Word"
        activate
        open myDoc
        set theActiveDoc to the active document
        save as theActiveDoc file format format PDF file name pdfSavePath
    end tell

      



I'm not an AppleScript expert. I had forward slashes instead of: as path separators. C: works

+2


source


The Joris Mans script is pretty good, but it has a flaw. This does not guarantee that Word is ready for the active document (a set theActiveDoc to the active document

missing value will be returned if possible)

I also improved the script to use the Finder selection as input by placing the PDF files in the same location as the word files. I haven't tested the file types, but Word will complain about this.



tell application "Finder"
    set input to selection
end tell

tell application id "com.microsoft.Word"
    activate
    repeat with aFile in input
        open aFile
        set theOutputPath to ((aFile as text) & ".pdf")
        repeat while not (active document is not missing value)
            delay 0.5
        end repeat
        set activeDoc to active document
        save as activeDoc file name theOutputPath file format format PDF
        close active document saving no
    end repeat
end tell

      

0


source







All Articles