AppleScript – Save Microsoft Word DOC(X) Document to PDF

From many scripts and apps that I have tested, this one is the only that works perfectly (and looks quite simple!)

tell application "Finder"
	set input to selection
end tell

tell application id "com.microsoft.Word"
	activate
	set view type of view of active window to draft view
	
	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

Tested with MS Word for Mac 16.44 on macOS Catalina.

Together with the KM macro to merge a folder with PDF files to one PDF, this is pure gold.

2 Likes

Thanks. This worked a treat.

I’ve recently duplicated and modified the script to convert the older .doc to the newer .docx format that my CAT tool can process: worked like a charm too.

1 Like

Excellent. Would you mind linking so I can shamelessly steal that as well?

tell application "Finder"
	set input to selection
end tell

tell application id "com.microsoft.Word"
	activate
	set view type of view of active window to draft view
	
	repeat with aFile in input
		open aFile
		set theOutputPath to ((aFile as text) & ".docx")
		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 document
		close active document saving no
	end repeat
end tell

Have fun with it :).

1 Like

Hi ALYB,

Thanks for posting this code, it gets me a part way to solving my problem.

I’m at the start of a huge learning curve with Applescript and feel way out of my depth. I’m sure what I’m trying to achieve is possible but I don’t know how to go about it.

I am trying to create a Droplet that will convert Word documents into pdf files and save them into a designated folder, not the same folder as the Word documents. Seems straightforward enough.

Google showed me your script that will save pdfs into the same folder as the Word documents but that’s not what I require and I don’t know how to change it.

Another problem is the script only works with straight forward Word documents. By that I mean the files I’m working with all have multiple links to external data in an Excel workbook. The links are set to automatically update. When the document opens, a prompt appears requiring a response. I don’t know how to write a script that answers the prompt. See image.

Screenshot 2022-02-18 at 20.13.31

All the files open but the script fails at the prompt, so I have to manually click ‘Yes’ for each one.

Ideally the script would answer ‘Yes’ (not ‘No’) to the prompt for each file in the list and then save a pdf into my designated folder before closing.

Another problem. The automatic update setting, triggers another prompt on closing the document, asking if I want to save changes or not; ideally yes. I don’t know how to write a script to answer this prompt either.

Screenshot 2022-02-18 at 20.21.57

Turning the ‘Update: automatically’ option on and off gets around the prompts but it’s not really a viable solution. In short, I'm looking for a Droplet that can answer both prompts appropriately and also save a pdf to my designated folder.

Please, if anyone can help me?

Looks like this is broken in Ms Word for Mac Version 16.70. I get the error:

"Microsoft Word got an error: active document doesn’t understand the “save as” message." number -1708 from active document

Let me ask at MacScripter if some kind should can fix this.

AppleScript support is broken in Word v16.70 (and the other Office apps of that update). Only Microsoft can fix this...