Page 1 of 1

Translate from VBA to COM?

Posted: 01 May 2017, 18:16
by Gibbons
This is what I have in VBA:

Code: Select all

Sub Macro2()
'
' Macro2 Macro
'
'
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf" _
        , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
    ActiveDocument.Close
    Application.Quit
End Sub
Here is what I have in AutoHotKey:

Code: Select all

q::
{
	oWord := ComObjActive("Word.Application")
	oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf","wdExportFormatPDF",True,"wdExportOptimizeForPrint","wdExportAllDocument",1,1,"wdExportDocumentContent",True,True,"wdExportCreateNoBookmarks",True,True,False)
	oWord.ActiveDocument.Close
    oWord.Application.Quit
	return
}
The line starting oWord.ActiveDocument.ExportAsFixedFormat doesn't work. The other lines work.

Any ideas on where I went wrong? Is there a better way to do this?

Thanks,
Gibbons

Re: Translate from VBA to COM?

Posted: 01 May 2017, 18:24
by Nightwolf85
It looks like you are using named constants, like wdExportFormatPDF, wdExportOptimizeForPrint, wdExportOptimizeForPrintetc. AHK doesn't know what values those are, you have to look up the numerical value for them and use that instead. Also sometimes you need to use the COMObject for True, not sure if you do in this case, but if you do then it would be ComObj(0xB,-1) in place of True.

Check out this guide, it will probably help you figure out what you need:
https://autohotkey.com/boards/viewtopic.php?t=8978

Edit
For this specific function you can check this site, you can click on each parameter and it will give what constants are, along with their values:
https://msdn.microsoft.com/en-us/librar ... 40962.aspx

Edit2
If the True doesn't need to be Object True then this should work I think:

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf",17,True,0,0,1,1,0,True,True,0,True,True,False)
And if it needs it then maybe this:

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf",17,ComObj(0xB,-1),0,0,1,1,0,ComObj(0xB,-1),ComObj(0xB,-1),0,ComObj(0xB,-1),ComObj(0xB,-1),ComObj(0xB,0))

Re: Translate from VBA to COM?

Posted: 01 May 2017, 18:45
by Gibbons
oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf",17 (&H11),ComObj(0xB,-1),0,0,1,1,0,ComObj(0xB,-1),ComObj(0xB,-1),0,ComObj(0xB,-1),ComObj(0xB,-1),False)

The above didn't seem to work either. I tried it with and without ComObj(0xB,-1) for true and with and without "" around 17 (&H11)

Re: Translate from VBA to COM?  Topic is solved

Posted: 01 May 2017, 18:47
by Nightwolf85
Hmm, I've never worked with Word before personally, so let me get a test document going and I'll help troubleshoot what is going on.

Edit
This seemed to have worked for me:

Code: Select all

q::
{
	oWord := ComObjActive("Word.Application")
	oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\User\Desktop\Test1.pdf",17,ComObj(0xB,-1),0,0,1,1,0,ComObj(0xB,-1),ComObj(0xB,-1),0,ComObj(0xB,-1),ComObj(0xB,-1),ComObj(0xB,0))
	oWord.ActiveDocument.Close
    oWord.Application.Quit
	return
}
It created a PDF of my word file, closed word and opened the PDF.

Edit2
Even with the regular True and False it seems to work for me:

Code: Select all

q::
{
	oWord := ComObjActive("Word.Application")
	oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\User\Desktop\Test1.pdf",17,True,0,0,1,1,0,True,True,0,True,True,False)
	oWord.ActiveDocument.Close
    oWord.Application.Quit
	return
}

Re: Translate from VBA to COM?

Posted: 01 May 2017, 19:02
by Gibbons
That worked for me also. The macro editor in Word gave me a value of 17 (&H11) for one input when it was actually must 17 that worked.

THANKS

So my first COM script started.

Is there a "best practices" method for knowing what all of those parameters mean? I suspect I'll just have to put the original VBA code in as a remark?

Re: Translate from VBA to COM?

Posted: 01 May 2017, 19:05
by Nightwolf85
Yeah, I usually just put a comment after the call with either a link to the site I used if one, or a duplicate fake call using the named parameters for future me to go back and know what is what.

Glad it's working.

Re: Translate from VBA to COM?

Posted: 01 May 2017, 19:17
by jeeswg
This might be useful (constants for Word and Excel):

MS Office COM Interface Constants - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=22&t=1672

I would #Include a script that contains all the constants, and write code like this:

Code: Select all

oWord.ActiveDocument.ExportAsFixedFormat("C:\Users\Gibbons\Desktop\Automation Project\Tests\Test1\Test1.pdf",wdExportFormatPDF,True,wdExportOptimizeForPrint,wdExportAllDocument,1,1,wdExportDocumentContent,True,True,wdExportCreateNoBookmarks,True,True,False)
To miss out a parameter, I would use ComObjMissing() or equivalent, although in AHK v2, you should just be able to miss out the parameter.

Code: Select all

m := ComObjMissing() ;AHK v1
m := ComObject(0xA, 0x80020004) ;AHK v1/v2
Btw '17 (&H11)' means '17 (0x11)', it's writing it in Dec and Hex.

==================================================

Here is some code to list the MS Office (Excel/Word) constants in a string, and retrieve their values, to check if they have been defined.

Code: Select all

;#Include, %A_ScriptDir%\Lib\z ms excel constants.ahk
;#Include, %A_ScriptDir%\Lib\z ms word constants.ahk

;the #Include files I used are based on:
;MS Office COM Interface Constants - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=22&t=1672

q:: ;check if MS Office (Excel/Word) constants are defined
vText := Clipboard
vText := RegExReplace(vText, "\W+", " ")
vList := "`n"
vOutput := ""
Loop, Parse, vText, % " "
{
	vTemp := A_LoopField
	;if (SubStr(vTemp, 1, 2) = "xl") ;Excel
	if (SubStr(vTemp, 1, 2) = "wd") ;Word
	&& !InStr(vList, "`n" vTemp "`n")
		vList .= vTemp "`n", vOutput .= vTemp " := " %vTemp% "`r`n"
}
vList := ""
Clipboard := vOutput
MsgBox, % vOutput
return
Your code had 5 constants:
wdExportFormatPDF
wdExportOptimizeForPrint
wdExportAllDocument
wdExportDocumentContent
wdExportCreateNoBookmarks

Although unfortunately, these ones were not listed in the link I posted. Nightwolf85 provided the link above, where they can be retrieved:

Document.ExportAsFixedFormat Method (Word)
https://msdn.microsoft.com/en-us/librar ... 40962.aspx