Create PDF from Word

Has anyone figured this out? I have been searching and all I can tell is that the function to print to file in Word does not work as it is supposed to. I can’t find any GUI script code that actually works either. Anyone have this working?

According to the dictionary this should work, but it doesn’t:

set PathToDesktop to path to the desktop as text
tell application "Microsoft Word"
	activate
	tell front document
		set myName to (name of the front document) as text
		set AppleScript's text item delimiters to {"."}
		set fileBase to first text item of (myName as string)
		set PS_file_path to (PathToDesktop & "PDF Folder:" & fileBase & ".ps") as text
		print out print out range print all document output file name PS_file_path with print to file without background
	end tell
end tell

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I don’t use word, so I can’t be specific but you can usually print anything with gui scripting like… here’s an example using safari with printing-to-pdf. You might have to tweak the “repeat 5 times, keystroke tab” part but it should work.

set myDelay to 1

tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
	keystroke "p" using command down
	delay myDelay
	repeat 5 times
		keystroke tab
		delay myDelay
	end repeat
	keystroke space
	delay myDelay
	keystroke (ASCII character 31) -- down arrow
	delay myDelay
	keystroke space
	delay myDelay
	keystroke "my file name"
	delay myDelay
	keystroke tab
	delay myDelay
	keystroke return
end tell

Awesome! I have not been able to figure out how to get that “PDF” button to activate. Spent hours with a bunch of click button 5 of group 1 of group 2… nonsense yesterday.

That’s awesome. I was able to make a drag and drop app that will batch create PDFs from Word files. Here it is if anyone else has a need:

on run
	with timeout of 9999 seconds
		display dialog "This script works by dragging and dropping a Word document or multiple Word documents onto the script icon." buttons "OK" default button "OK" with icon 2 giving up after 9990
	end timeout
end run

on open these_items
	tell application "Microsoft Word" to launch
	tell application "Finder"
		activate
		if not (exists folder "PDF Folder") then
			make new folder at desktop with properties {name:"PDF Folder"}
		end if
	end tell
	
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
	tell application "Finder"
		activate
		try
			open folder "PDF Folder"
		end try
		beep 1
		display dialog ("Finished!") buttons " " giving up after 1
	end tell
end open

-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine prints the files
on process_item(this_item)
	tell application "System Events"
		set the item_info to info for this_item
		if (kind of item_info starts with "Microsoft Word") then
		else
			with timeout of 9999 seconds
				display dialog "This file is not a Word document!" buttons "Cancel" default button "Cancel" with icon 0 giving up after 9990
			end timeout
		end if
	end tell
	tell application "Microsoft Word"
		activate
		open this_item
		delay 2
		tell front document
			set myName to (name of the front document) as text
			set AppleScript's text item delimiters to {"."}
			set fileBase to first text item of (myName as string)
			tell application "System Events"
				keystroke "p" using command down
				delay 1
				keystroke tab
				delay 1
				repeat 6 times
					keystroke tab using shift down
					delay 0.5
				end repeat
				keystroke space
				delay 1
				repeat 4 times
					key code 125
					delay 0.5
				end repeat
				key code 36
				delay 4
				keystroke "d" using command down
				delay 0.5
				keystroke (fileBase & ".pdf")
				repeat 7 times
					keystroke tab
					delay 0.5
				end repeat
				key code 126
				keystroke "pdf"
				delay 0.5
				keystroke "o" using command down
				delay 1
				key code 36
			end tell
		end tell
		close front document saving no
	end tell
end process_item