4
votes

I am going nuts here. I have tried countless permutations/variations of "save as active document file format PDF" but none seem to work. I get AppleScript errors with all of them.

So can anyone tell me:

What is the exact syntax to save the active document as a PDF file in AppleScript, using Word?

It seems that there is no coherence whatsoever in the Office for Mac scripting, as I have this working 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?

Thanks!

2

2 Answers

3
votes

It seems I found it after all:

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 am no AppleScript expert. I had slashes instead of : as path separators. With : it works

0
votes

Joris Mans script is pretty nice, but has a flaw. It does not ensure that Word has an active document ready (set theActiveDoc to the active document might return missing value)

I also improved the script to use the Finder selection as input, placing the PDFs into the same place as the word files. I did not test the file types, but word will complain about that.

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