Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Save as PDF using applescript in Word 365

This is wasting hours of my life!!


This worked fine in word 2011 but now that I have been forced to upgrade to 365 it no longer saves as pdf:


tell application "Microsoft Word"

open "/users/raft/desktop/esd.docx"

set theActiveDoc to the active document

save theActiveDoc in "/users/raft/desktop" as format PDF

close front document of application "Microsoft Word" saving no

end tell


Please help. I just can't figure this out!

Posted on Jan 6, 2019 9:39 AM

Reply
Question marked as Best reply

Posted on Jan 6, 2019 3:28 PM

I thought I posted a AppleScript solution to you earlier, but apparently not. I don't have Word installed, so could not compile and run the following AppleScript.


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


8 replies
Question marked as Best reply

Jan 6, 2019 3:28 PM in response to rafty

I thought I posted a AppleScript solution to you earlier, but apparently not. I don't have Word installed, so could not compile and run the following AppleScript.


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 6:21 PM in response to VikingOSX

You are clearly a master! Works perfectly. I only have one more issue which I suspect if you don't use word you would not know work around. With Word 2016 and later, when I fire the open file and save as pdf applescript, I get a permission granted request dialog. This only occurs the first time I am trying to access the folder to save to and I am unable to automate the permission to grant. Any ideas?



Jan 6, 2019 7:26 PM in response to VikingOSX

I am presently running the latest version of Work (16.20). I also came across this article however it appears to be a different problem. In the article users are unable to open the file at all whereas in my case it is simply granting permission to access the folder. I could not see anything in the preferences section of word to overcome this.

Jan 7, 2019 4:18 AM in response to rafty

I went searching for an answer to this grant permission problem on Google. It is widespread, and I did not read any specific article across several sources that had a definitive solution to this long-running problem. I suggest interaction in Microsoft's Office for Mac Community. Select your specific Office product category on that page.

Save as PDF using applescript in Word 365

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