Saving Word document as PDF

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Micha
Posts: 50
Joined: 15 Oct 2015, 04:54

Saving Word document as PDF

Post by Micha » 26 Mar 2016, 05:11

I have code to replace phrases in Microsoft Word, which works fine. The document is then saved. That works fine. Then I want to save it as a PDF file. That doesn't work, and probably because I have a partial path and a further path and have to mix them.

What I am trying to do:

; pdfpath := path . "\Further folder\Document.pdf"
; oWord.ActiveDocument.SaveAs2(pdfpath, 17)

I'm not sure why "17" is there in the second command, I got this code from an earlier Autohotkey Forum post.

It's probably not relevant, but the full code here is:

Code: Select all

WordMultiReplace("Stuff", "Other stuff")

WordMultiReplace(params*) {	
	oWord := ComObjActive("Word.Application")
	oWord.Selection.Find.ClearFormatting
	oWord.Selection.Find.Replacement.ClearFormatting	
	For k,v in Params
	{
		c++
		if (c = 1)
		{
			st := v
			continue
		}
		rt := v, c := 0
		oWord.Selection.Find.Execute(st, 0, 0, 0, 0, 0, 1, 1, 0, rt, 2)
	}	
	oWord.ActiveDocument.SaveAs
	pdfpath := path . "\Further folder\Document.pdf"
        oWord.ActiveDocument.SaveAs2(pdfpath, 17)
	WinClose
}

return
Any idea as to why I can't save this as a PDF document in the relevant folder (or at all)?

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Saving Word document as PDF

Post by boiler » 26 Mar 2016, 06:25

The 17 identifies the file type as PDF.

You don't assign a value to the variable "path" anywhere in your script, so "pdfpath" is not a full path name.

Micha
Posts: 50
Joined: 15 Oct 2015, 04:54

Re: Saving Word document as PDF

Post by Micha » 26 Mar 2016, 06:51

Sorry, I forgot to mention that "path" is set equal with the full path to a folder further up in the program. This is a part of a very large set of code. I printed out "path" with a MsgBox, and it does hold a path.

just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Saving Word document as PDF

Post by just me » 26 Mar 2016, 08:05

Might be a silly question, did you declare path as super-global? Otherwise the function cannot access it.

Micha
Posts: 50
Joined: 15 Oct 2015, 04:54

Re: Saving Word document as PDF

Post by Micha » 26 Mar 2016, 08:25

just me: That did the trick. Thank you!

sachin24
Posts: 9
Joined: 19 May 2020, 07:25

Re: Saving Word document as PDF

Post by sachin24 » 10 Jun 2021, 15:05

I want to export only some pages (Range like 2-5) of word file. Is it possible. I don't want entire document to be converted into pdf. :angel:
boiler wrote:
26 Mar 2016, 06:25
The 17 identifies the file type as PDF.

You don't assign a value to the variable "path" anywhere in your script, so "pdfpath" is not a full path name.

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Saving Word document as PDF

Post by boiler » 10 Jun 2021, 17:25

This exports the range of pages from 2 to 5 as a PDF. Leave the other parameters (17 and 3) as shown and described in the comment.

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat2(pdfpath, 17,,, 3, 2, 5) ; 17=wdExportFormatPDF, 3=wdExportFromTo

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: Saving Word document as PDF

Post by Spitzi » 28 Sep 2022, 02:33

Hi @boiler.

I tried using ExportAsFixedFormat2, but it seems that only ExportAsFixedFormat is a valid function. If i run the code:

Code: Select all

filePath := PdfFolderPath . "\Befund_" . patientNameBirthdate . ".pdf"
filePath2 := PdfFolderPath . "\Befund_" . patientNameBirthdate . "_HiFi.pdf"
oWord.ActiveDocument.ExportAsFixedFormat(filePath, 17,,, 3,1,100) 																						
oWord.ActiveDocument.ExportAsFixedFormat2(filePath2, 17,,, 3,1,100) 																	
I get an error "unknown name" specifically:ExprtAsFixedFormat2

Do you know why? Greets Spitzi


Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: Saving Word document as PDF

Post by Spitzi » 28 Sep 2022, 02:57

Thanks @Xtra, I've read the documentation. But running my code does not work, ahk throws an "unknown name" error... I don't know why

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: Saving Word document as PDF

Post by Spitzi » 28 Sep 2022, 03:31

Hmm, maybe it has to do with me using Office 2019. It doesn't say in the documentation, when exportasfixedformat2 was introduced, unfortunately. But maybe the function does simply not exist yet in Office 2019.

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Saving Word document as PDF

Post by boiler » 28 Sep 2022, 04:30

Spitzi wrote: Hmm, maybe it has to do with me using Office 2019. It doesn't say in the documentation, when exportasfixedformat2 was introduced, unfortunately. But maybe the function does simply not exist yet in Office 2019.
That is not the case as it has been in use for years before 2019:
https://stackoverflow.com/questions/37585025/difference-between-exportasfixedformat2-and-exportasfixedformat

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: Saving Word document as PDF

Post by Spitzi » 28 Sep 2022, 04:34

Thanks, @boiler .

Ok, so it is not that.... any idea why I get the error with "ExportAsFixedFormat2", but not with "ExportAsFixedFormat". Do I need a newer AHK version?

Greets Spitzi

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Saving Word document as PDF

Post by boiler » 28 Sep 2022, 04:43

The AHK version should have nothing to do with it since the methods are part of the Word objects and aren’t part of the AHK code (i.e., ExportAsFixedFormat isn’t coded into AHK either). Go ahead and update to the latest release (1.1.34.04) to prove that’s not the issue.

Looking at your example code, you never have established the Word object. Is that your complete code? You have to use some thing like:

Code: Select all

oWord := ComObjActive("Word.Application")
…or you can’t make use of anything discussed in this thread.

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: Saving Word document as PDF

Post by Spitzi » 28 Sep 2022, 05:31

This is the whole code:

Code: Select all

#SingleInstance Force																								
#NoEnv  		

global TempFolderPath := "C:\Temp"																					
global PdfFolderPath := TempFolderPath . "\" . A_UserName . "\reports"												

oWord := ComObjActive("Word.Application")
filePath := PdfFolderPath . "\Befund.pdf"
filePath2 := PdfFolderPath . "\Befund_HiFi.pdf"
oWord.ActiveDocument.ExportAsFixedFormat(filePath, 17,,, 3,1,100) 	
oWord.ActiveDocument.ExportAsFixedFormat2(filePath2, 17,,, 3,1,100)

oWord := ""
I get an error on the second last line, also with the latest AHK version 1.1.34. The first export works fine.

Any thoughts?

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Saving Word document as PDF

Post by boiler » 28 Sep 2022, 06:16

Perhaps it is for some reason not implemented in Word 2019. Like after a certain point, maybe it is only implemented in Office 365/Microsoft 365 or something like that. I don’t have a way of testing the other versions.

Post Reply

Return to “Ask for Help (v1)”