Newbie grappling with COM

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cstarkers
Posts: 4
Joined: 09 Sep 2018, 11:08

Newbie grappling with COM

09 Sep 2018, 11:32

Hi all, first time poster, nearly-first time scripter here, please excuse any stoopidity :crazy:

I'm writing a script to automate some repetitive admin stuff at work. The first step is printing an email from Outlook to a PDF (I'm using Word instead of Outlook here, because I don't use Outlook at home, but its exactly the same). I've done some digging and figured out how to change the default printer, and then use COM to print. So far so good.

The problem is that Print to PDF brings up a window to select where you want to save the PDF, but it doesn't end the Printout method until you get rid of that window. I want to have my script interact with that window, and select the destination for me. How do I get it to ignore the fact that the method isn't completed, and proceed to the Click regardless?

Code: Select all

Lwin & p::
	
	SetDefaultPrinter(Microsoft Print to PDF)
	ItemToPrint := ComObjActive("Word.Application").ActiveDocument
	ItemToPrint.Printout
	
	Click, Edit2, Save Print Output As ahk_exe WINWORD.EXE
	


SetDefaultPrinter(sPrinter)
{
	DllCall(A_WinDir . "\system32\winspool.drv\SetDefaultPrinter", "str", sPrinter)
} 
I've tried using SetTimer with a label after the "ItemToPrint.Printout", but that didn't work :(
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

Re: Newbie grappling with COM

09 Sep 2018, 17:00

start the timer before the ItemToPrint.Printout

Code: Select all

	SetDefaultPrinter(Microsoft Print to PDF)
	ItemToPrint := ComObjActive("Word.Application").ActiveDocument
	
	;start the timer here
	ItemToPrint.Printout
	
	Click, Edit2, Save Print Output As ahk_exe WINWORD.EXe
	
	
cstarkers
Posts: 4
Joined: 09 Sep 2018, 11:08

Re: Newbie grappling with COM

09 Sep 2018, 21:55

No dice I'm afraid grimboto.

Code: Select all

	ItemToPrint := ComObjActive("Word.Application").ActiveDocument

	setTimer, a, -1000 ;as suggested
	
	ItemToPrint.Printout
	
	exitapp; This serves no function, I just wanted to confirm that this would never execute, thereby confirming that the timer was actually starting the "a" sub
	 
	a:
	msgbox success  ; I added a message box in case my click was just clicking empty space or something
	Click, Edit2, Save Print Output As ahk_exe WINWORD.Exe
Result is the same thing. The subroutine "a" is only executed after I manually cancel the Print to PDF dialogue. One weird thing of note is that the timer is obviously running while the dialogue is open, because msbox appears instantly once I cancel the dialogue. It just seems that the timer is unable to interrupt the COM method.
cstarkers
Posts: 4
Joined: 09 Sep 2018, 11:08

Re: Newbie grappling with COM

09 Sep 2018, 22:34

I have found a solution! All I had to do was run a second script to monitor for the print dialouge window, and have it take over for a little bit, much like this: https://autohotkey.com/boards/viewtopic.php?f=5&t=26828

It's not exactly elegant, and if anyone has a better way of handling this please let me know (it still baffles me that SetTimer doesn't seem to work), but for now that will get the job done.
aifritz
Posts: 301
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: Newbie grappling with COM

10 Sep 2018, 00:51

An other approach could be, to save the document from Word as pdf. There appears no file dialog box...
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Newbie grappling with COM

10 Sep 2018, 03:10

The PrintOut method has the OutputFileName parameter you can specify. See https://docs.microsoft.com/en-us/office ... n.printout

Example:

Code: Select all

wd := ComObjActive("Word.Application")
wd.ActivePrinter := "Microsoft Print to PDF"
wd.ActiveDocument.PrintOut(,,, "D:\Desktop\test.pdf")
MsgBox, Print Finished
cstarkers
Posts: 4
Joined: 09 Sep 2018, 11:08

Re: Newbie grappling with COM

10 Sep 2018, 03:54

Thanks very much Aifritz and Tmplinshi. I am going to stick with the two scripts approach - I'm only using Word as an analogue while I develop this script at home; when it's running at work it will be with Outlook, I just don't use Outlook at home and didn't want to set it up just to test. The outlook version of Printout doesn't accept any parameters, and just prints it with all defaults.

Same goes for the "save as PDF option," it doesn't exist in Outlook.
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Newbie grappling with COM

10 Sep 2018, 09:54

cstarkers wrote:Hi all, first time poster, nearly-first time scripter here, please excuse any stoopidity :crazy:

I'm writing a script to automate some repetitive admin stuff at work. The first step is printing an email from Outlook to a PDF (I'm using Word instead of Outlook here, because I don't use Outlook at home, but its exactly the same). I've done some digging and figured out how to change the default printer, and then use COM to print. So far so good.

The problem is that Print to PDF brings up a window to select where you want to save the PDF, but it doesn't end the Printout method until you get rid of that window. I want to have my script interact with that window, and select the destination for me. How do I get it to ignore the fact that the method isn't completed, and proceed to the Click regardless?

Code: Select all

Lwin & p::
 
 SetDefaultPrinter(Microsoft Print to PDF)
 ItemToPrint := ComObjActive("Word.Application").ActiveDocument
 ItemToPrint.Printout
 
 Click, Edit2, Save Print Output As ahk_exe WINWORD.EXE
 


SetDefaultPrinter(sPrinter)
{
 DllCall(A_WinDir . "\system32\winspool.drv\SetDefaultPrinter", "str", sPrinter)
} 
I've tried using SetTimer with a label after the "ItemToPrint.Printout", but that didn't work :(
You can do it with one script. Before Click, use WinWait or WinWaitActive to wait for the Print to PDF window.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Newbie grappling with COM

10 Sep 2018, 19:04

cstarkers wrote:I'm writing a script to automate some repetitive admin stuff at work. The first step is printing an email from Outlook to a PDF
This code will export the current open email in Outlook to a PDF file:

Code: Select all

F11::
	olApp := ComObjActive("Outlook.Application")
	olInspector := olApp.ActiveInspector.WordEditor.Application.ActiveDocument
	FilePathName := A_ScriptDir "\" RegExReplace(olInspector.Name, "[\?<>/\\\*""|:]") ".pdf"
	olInspector.ExportAsFixedFormat(FilePathName, 17)
return
Outlook does not do PDF, so this works through Word which is the editor used by Outlook. When you open an email in Outlook then Word gets involved and takes over the displaying and editing. This then makes it easy to use Word COM methods.

If you need to work on emails that are not open then probably the simplest solution is to just have the script open then, use the above code, and then close them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Xtra and 273 guests