better way to make a pdf from a Word docx Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
AmDeG 11
Posts: 358
Joined: 28 Nov 2013, 11:42

better way to make a pdf from a Word docx

19 Dec 2019, 20:31

the following script works. It saves a Word doc then creates a pdf from that Word doc and closes both.

Is there a better way to do it?

Code: Select all

+^!z:: ;save docx as pdf replacing old one with last one if needed
       Send ^s
       Sleep 1500
       Send {F12}    ;FileAs
       Sleep 300
       Send !t  
       Sleep 200
       Send p{Return}
       Sleep 300
       Send y{Return}
       Sleep 1000
       WinClose ahk_class AcrobatSDIWindow
       Sleep 1000
       WinClose ahk_class OpusApp
     ;  Sleep 200
       Send {Return}
    Return
User avatar
TheDewd
Posts: 1391
Joined: 19 Dec 2013, 11:16
Location: USA

Re: better way to make a pdf from a Word docx

23 Dec 2019, 09:52

This code should create a PDF from the active word document, saving the PDF in the same directory as the opened doc.

Code: Select all

#SingleInstance, Force

oWord := ComObjActive("Word.Application")
oWord.ActiveDocument.SaveAs2("Document.pdf", 17)
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
AmDeG 11
Posts: 358
Joined: 28 Nov 2013, 11:42

Re: better way to make a pdf from a Word docx

23 Dec 2019, 12:14

should I add anything else?

it is not working
aifritz
Posts: 237
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: better way to make a pdf from a Word docx

23 Dec 2019, 12:48

Not working could be everything and nothing... :crazy:
What error do you get?

Also have a look here, if you are using a prior version of word 2010:

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=7332&hilit=ActiveDocument.SaveAs2
User avatar
flyingDman
Posts: 756
Joined: 29 Sep 2013, 19:01

Re: better way to make a pdf from a Word docx

23 Dec 2019, 14:34

It works here.
Instead of saveas2 you can also use ExportAsFixedFormat like:

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat("document.pdf",17)
AmDeG 11
Posts: 358
Joined: 28 Nov 2013, 11:42

Re: better way to make a pdf from a Word docx

23 Dec 2019, 18:18

it does not work for me :shifty:

I have the latest version of Word

And I don't get any error. It simply doesn't work
Last edited by AmDeG 11 on 23 Dec 2019, 18:35, edited 1 time in total.
User avatar
flyingDman
Posts: 756
Joined: 29 Sep 2013, 19:01

Re: better way to make a pdf from a Word docx

23 Dec 2019, 18:26

Try:

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat("document.pdf",17,1)
AmDeG 11
Posts: 358
Joined: 28 Nov 2013, 11:42

Re: better way to make a pdf from a Word docx

23 Dec 2019, 18:39

now it works

in the sense that it does open a pdf window with the file in pdf format

however it does not automatically saves it at the same folder of the original Word file (which would be great too!)

the best would be that I see just a little that the pdf was done and then close knowing that I'll have it at my folder next to the Word original
User avatar
flyingDman
Posts: 756
Joined: 29 Sep 2013, 19:01

Re: better way to make a pdf from a Word docx

23 Dec 2019, 18:50

If it opened it means that the file is on your drive. Just look for it (in the same folder as your script maybe?). You can save it in whatever path you want.
To retrieve the path of the the word document use oword.Activedocument.path
AmDeG 11
Posts: 358
Joined: 28 Nov 2013, 11:42

Re: better way to make a pdf from a Word docx

24 Dec 2019, 00:54

sorry I'm at a loss in order to save the file in the same folder as the Word file

+^s opens de Save As dialog

then I send a Return to open it but I don´t how it works with the oword.Activedocument.path
User avatar
flyingDman
Posts: 756
Joined: 29 Sep 2013, 19:01

Re: better way to make a pdf from a Word docx  Topic is solved

24 Dec 2019, 13:04

The following should save the .pdf in the same folder as the .docx file wherever the latter is located.

Code: Select all

oWord := ComObjActive("Word.Application")
fullname := oword.Activedocument.fullname
SplitPath, fullname , , OutDir, , OutNameNoExt
oWord.ActiveDocument.ExportAsFixedFormat(OutDir "\" OutNameNoExt ".pdf",17)    
return	

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], Clemens375, euras, Google [Bot], songdg and 245 guests