applescript save as pdf microsoft word 365

Hi,


I have been using an applescript to:

  1. open a word document
  2. Save the word document as a pdf
  3. Close the work document

This work fine in older version of word, however in 365 it no longer understands the command "save as pdf"


Please would someone put my out of my misery and provide me an example of an applescript doing this.


Thanks

Posted on Jan 6, 2019 7:06 AM

Reply
6 replies

Jan 6, 2019 10:45 AM in response to rafty

I don't have MS Office installed to compile and test the following AppleScript beyond the tell application statement, so give this a whirl, and see if it works. Supposedly, this is the Word 2016 for Mac Scripting dictionary syntax. As written, it saves the PDF in the same location as the original Word document. It should process both .docx and .doc files.


property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}
property default_path : (path to desktop) as alias
property Delim : {".docx", ".doc"}
property PDF : ".pdf"

set outPDF to {}

set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents)

-- replace Word document extensions with PDF, and append to outPDF list
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim}
repeat with afile in selected_files
	copy item 1 of text items of (afile as text) & PDF to the end of outPDF
end repeat
set AppleScript's text item delimiters to TID

tell application id "com.microsoft.Word"
activate
repeat with i from 1 to count of selected_files
open (item i of selected_files)
set theOutputPath to (item 1 of outPDF)
-- close access (open for access exportDocument)
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
return



Jan 6, 2019 7:44 AM in response to rafty

Open Script Editor. In the File menu, Open Dictionary… and look for the appropriate version of Word used from Office 365 in the list of applications with scripting dictionaries. The syntax that you need to produce a PDF should be in that scripting dictionary entry for Word.


You can also achieve this without opening and saving the Word document by using LibreOffice from the Terminal or even in an AppleScript via a do shell script invocation:


/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf sample.docx

Jan 6, 2019 8:11 AM in response to VikingOSX

Thank you for this quick response.


I have looked at the dictionary for Office Word 365 however it does not have a pdf option but rather:


savev : Save a document.

save specifier : The document(s) or window(s) to save.

[in file] : The file in which to save the document.

[as saveable file format] : The file format to use.


So when I do:


tell "Microsoft Word"

save in "/Users/XXX/Desktop/ESD.docx" as PDF

end tell


I receive and error "variable PDF is not defined".


Regarding your second suggestions using LibreOffice. This seems much quicker - only thing I can't figure out is how to tell it where to save the PDF file. It always defaults to the user directory.


Thanks in advance.

Jan 6, 2019 9:11 AM in response to VikingOSX

All these other methods bring in a number of formatting and other issues. This is the applescript working before:


tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path

tell application "Microsoft Word"

set default file path file path type documents path path pdfSavePath

open myDoc

set theActiveDoc to the active document

save as theActiveDoc file format format PDF file name "FILENAME.pdf"

close front document of application "Microsoft Word" saving no

end tell

tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath


This no longer works with Word 365. Any ideas?

Jan 6, 2019 8:32 AM in response to rafty

I don't have any Office for Mac installed, so cannot explore that Word output to PDF further for you. LibreOffice is pretty competent about its Word .docx support. I keep the most recent version of LibreOffice installed as a habit.


The default save location is the input file location. Otherwise:


/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir ~/Desktop ~/Documents/sample.docx


I used the tilde shorthand for the Desktop directory, but you can use tilde or full paths to the output location of your choice.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

applescript save as pdf microsoft word 365

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.