[Word COM] Printing document--print more than 1 copy

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

[Word COM] Printing document--print more than 1 copy

30 Aug 2017, 18:11

Hi folks,

I had the following code working to print custom copies of word documents. However, it has recently stopped working. The problem is, it used to work, so I must have changed something. It seems simple, and everything should work, but I have come to the end of my rope, and do not know the way further.

According to the 4th line, it will print 4, right? For me it prints 1 copy. Can anyone take a look at this and tell me if this code does the same for you? And provide pointers for troubleshooting?

Thanks in advance.

Code: Select all

printpath := "C:\Temp\blank.docx" ; blank word document to save toner
oWord := ComObjCreate("Word.Application") ; create MS Word object
oFile := oWord.Documents.Open(printpath) ; create new document
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt (because I can deal with the consequences myself if it doesn't print right, which it won't)
oFile.PrintOut( Copies:=4, Background:=false ) 
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference
try it and see
...
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 12:44

Bump. This code is way to simple to be doing something wrong. Can someone confirm whether or not this code works for them? Or give pointers for what might be the issue? Sorry for being impatient.
try it and see
...
Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 13:21

What about:

Code: Select all

oFile.PrintOut(,,,,,,,4)
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 13:43

Hmmm. That did more than just one... It printed 4², 16. Interestingly, this is what happened when I had a custom macro in Word, with the command Application.PrintOut(copies:=4). This would also print 16. 3 would print 9, etc. So I wrote up this AHK COM code, and it worked, until now it only prints 1.

Should I blame my print driver? Or my entire operating system? I'm confused, to say the least.
try it and see
...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 13:43

Nightwolf85 wrote:What about:

Code: Select all

oFile.PrintOut(,,,,,,,4)
This points out an important consideration when converting VBA to AHK.
VBA allows named parameters, while with AHK every parameter has to be in its proper slot in the function call which can make for a lot of blank parameter slots when you only want to use one that is deep in the order.

This link shows the parameter order for PrintOut
https://msdn.microsoft.com/en-us/librar ... ntout.aspx

A lot of times in AHK, you will see people do stuff like: oFile.PrintOut(,,,,,,,Copies:=4) but the Copies:= is just superfluous to try to show clarity. A type of working comment that has no real effect.

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
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 13:48

Interesting FanaticGuru. So I wonder why I thought this was working before. I am starting to doubt myself. Maybe it wasn't.

I am just stumped, though, what is multiplying the number of copies by itself.
try it and see
...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 14:09

derz00 wrote:Hmmm. That did more than just one... It printed 4², 16. Interestingly, this is what happened when I had a custom macro in Word, with the command Application.PrintOut(copies:=4). This would also print 16. 3 would print 9, etc. So I wrote up this AHK COM code, and it worked, until now it only prints 1.

Should I blame my print driver? Or my entire operating system? I'm confused, to say the least.
Looking at the link in my above post it shows each parameter as being a System.Object not a simple string or integer as one would expect. Ain't that fun!

You can probably use something like ParamObj := ComObject(VarType, Value [, Flags]) to create the ParamObj.

Not sure of the VarType but here is a list of them:
https://msdn.microsoft.com/en-us/librar ... s.60).aspx

For example ComObject(0xB,-1) is a COM object Boolean true. 0xB is hex 11, -1 is binary logic true.

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
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 14:18

derz00 wrote:Interesting FanaticGuru. So I wonder why I thought this was working before. I am starting to doubt myself. Maybe it wasn't.
It could have been working by accident. You were sending it parameters in slots 1 and 2 that were not the right types that were being processed in odd ways combined with it using defaults some type of printout was occuring. Nothing you sent in the first two parameters should have effected the number of copies though.

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
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 16:02

FanaticGuru wrote: Not sure of the VarType but here is a list of them:
https://msdn.microsoft.com/en-us/librar ... s.60).aspx

For example ComObject(0xB,-1) is a COM object Boolean true. 0xB is hex 11, -1 is binary logic true.

FG
Thanks for the expert tips. I tried several of those types, and the following 2 did not throw an error/crash the program, but they both print 16.

Code: Select all

ParamObj := ComObject(0x3, 4) ; vbLong := 3
ParamObj := ComObject(0x2, 4) ; vbInteger := 2
I'll keep playing around as I have time, to try in my limited experience to source the issue.
try it and see
...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

31 Aug 2017, 16:43

Well after sending you on a wild goose chase with the ComObject. I actually ran some code and I can confirm on my system the below works as expected and prints 2 copies.

Code: Select all

printpath := A_Desktop "\Test\Blank.docx" ; blank word document to save toner
oWord := ComObjCreate("Word.Application") ; create MS Word object
oFile := oWord.Documents.Open(printpath) ; create new document
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt (because I can deal with the consequences myself if it doesn't print right, which it won't)
oFile.PrintOut(,,,,,,,2)
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference
The parameters automatically get converted properly to the right type object. Working with Excel, I know that they usually do but sometimes do not and you have to use ComObject for some parameters.

So I would guess it is something particular to your setup. Maybe something that changed and broke your working code althrough I don't see how you could have ever got more than one copy in AHK without leaving the proper number of blank parameter slots before the Copies parameter.

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
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: [Word COM] Printing document--print more than 1 copy

06 Sep 2017, 09:39

FanaticGuru wrote: So I would guess it is something particular to your setup. Maybe something that changed and broke your working code althrough I don't see how you could have ever got more than one copy in AHK without leaving the proper number of blank parameter slots before the Copies parameter.

FG
From what I can see, it was some glitch between the print driver and the printer. I researched it, and found a suggestion to enable "Mopier Mode". Researching that mode explained why the number of copies would be multiplied times itself.

Anyway, this code works with Mopier Mode enabled.

Code: Select all

	oWord := ComObjCreate("Word.Application") ; create MS Word object
	oWord.Visible := 0 ; unecessary. handy for turning on for debug purposes
	oFile := oWord.Documents.Open(printpath) ; create new document
	oWord.DisplayAlerts := 0 ; turns off alerts
	oFile.PrintOut(0,,,,,,,NumCopies) ; if it prints in the background, 
						   	        ; it does interfere with closing the 
						   	        ; application, especially if printing many copies
	oWord.DisplayAlerts := -1 ; turns them back on
	oFile.Close ; close the file
	oWord.Quit  ; close the application
Thanks again everyone for your willing help. I have version of this function on github. https://github.com/derz00/PrintDoc-File-Quanity
try it and see
...
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: [Word COM] Printing document--print more than 1 copy

13 Sep 2019, 13:10

FanaticGuru wrote:This link shows the parameter order for PrintOut
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.printout.aspx
The above documentation link was for "interop" ie: C#
So that's why everything was listed as an object.
FanaticGuru wrote: Looking at the link in my above post it shows each parameter as being a System.Object not a simple string or integer as one would expect. Ain't that fun!

You can probably use something like ParamObj := ComObject(VarType, Value [, Flags]) to create the ParamObj.
FanaticGuru wrote:The parameters automatically get converted properly to the right type object. Working with Excel, I know that they usually do but sometimes do not and you have to use ComObject for some parameters.
This is the correct documentation link:
https://docs.microsoft.com/en-us/office/vba/api/word.document.printout

You generally only need to use ParamObj := ComObject(VarType, Value [, Flags]) when you are getting a "type mismatch" error.
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: [Word COM] Printing document--print more than 1 copy

04 Jan 2021, 07:13

Hi @FanaticGuru and @derz00 ,
This is a fantastic script.

How would you get it to:
-print in color
-print double sided



thanks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

05 Jan 2021, 17:30

JKnight_xbt33 wrote:
04 Jan 2021, 07:13
How would you get it to:
-print in color
-print double sided

expression.PrintOut (Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)

https://docs.microsoft.com/en-us/office/vba/api/word.document.printout

I have read that you can change the " ManualDuplexPrint" property to false to print two sided. Have not actually tried it.

The color is a more difficult problem. That is controlled by the printer properties which I don't believe you can easily access. Usually it defaults to color so that if the document has colors it prints them. Usually it is the other way around where you want to print a color document in black & white. You can choose printers, so maybe you could create two printer devices and set one to color and the other to b&w and then choosing the printer you want.

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
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: [Word COM] Printing document--print more than 1 copy

07 Jan 2021, 10:13

@FanaticGuru
Thanks, regarding the color my workplace actually has it setup like you suggest i.e. color or black/white appear as two separate devices.

reading the microsoft documentation there doesn't appear to be a parameter to select which printer the doc goes to.

Do you know any alternative VBA references to select a device/printer?

thanks
J
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

07 Jan 2021, 15:19

JKnight_xbt33 wrote:
07 Jan 2021, 10:13
@FanaticGuru
Thanks, regarding the color my workplace actually has it setup like you suggest i.e. color or black/white appear as two separate devices.

reading the microsoft documentation there doesn't appear to be a parameter to select which printer the doc goes to.

Do you know any alternative VBA references to select a device/printer?

ActivePrinter
https://docs.microsoft.com/en-us/office/vba/api/excel.application.activeprinter

Code: Select all

printpath := A_Desktop "\Test\Blank.docx" ; blank word document to save toner
oWord := ComObjCreate("Word.Application") ; create MS Word object
oFile := oWord.Documents.Open(printpath) ; create new document
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt (because I can deal with the consequences myself if it doesn't print right, which it won't)
oWord.ActivePrinter := "Adobe PDF"
oFile.PrintOut(,,,,,,,2)
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference

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
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: [Word COM] Printing document--print more than 1 copy

11 Jan 2021, 09:59

Hi @FanaticGuru ,
Your script worked nicely for double sided print [Edit: 13/01/2021 doesn't print a true double sided copy, only manual duplex]

I just cant seem to get the active printer to work (line 5). Is there a specific naming convention I should use?

Code: Select all

printpath := A_Desktop "\Check List.docx" 
oWord := ComObjCreate("Word.Application") 
oFile := oWord.Documents.Open(printpath)
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt 
oWord.ActivePrinter := "FindMeColour on gbcamsvrprn01" ;doesn't seem to be working
oFile.PrintOut(,,,,,,,1,,,,,,1)
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference
return
here's a picture of the device with its name
color.PNG
color.PNG (20.7 KiB) Viewed 1417 times
Last edited by JKnight_xbt33 on 13 Jan 2021, 05:23, edited 1 time in total.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Word COM] Printing document--print more than 1 copy

11 Jan 2021, 17:05

JKnight_xbt33 wrote:
11 Jan 2021, 09:59
I just cant seem to get the active printer to work (line 5). Is there a specific naming convention I should use?

Code: Select all

printpath := A_Desktop "\Check List.docx" 
oWord := ComObjCreate("Word.Application") 
oFile := oWord.Documents.Open(printpath)
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt 
oWord.ActivePrinter := "FindMeColour on gbcamsvrprn01" ;doesn't seem to be working
oFile.PrintOut(,,,,,,,1,,,,,,1)
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference
return

It works for me to use the exact displayed name like you did.

You might try making the printer you want active manually then put MsgBox % oWord.ActivePrinter in the code right before and after you try to change it to see what is going on.

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
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: [Word COM] Printing document--print more than 1 copy

13 Jan 2021, 05:22

Hi @FanaticGuru ,
It printed in colour, turns out the syntax of the name of the printer was different.

Regarding printing double sided it turns out it doesn't work like I thought.

When i went ahead to print more than 2 pages it prints odd and even numbers as separate documents (which is what manuel duplex is)

I'll look on the forums to see if there is a vba reference to allow me to print a true double sided document.

here's the code so far

Code: Select all

printpath := A_Desktop "\Check List.docx" 
oWord := ComObjCreate("Word.Application") 
oFile := oWord.Documents.Open(printpath)
oWord.DisplayAlerts := 0 ; turns off alerts, so that "your margins are too small" warnings don't interrupt 
oWord.ActivePrinter := "\\private\FindMeColour" ;doesn't seem to be working
MsgBox % oWord.ActivePrinter
;oFile.PrintOut(,,,,,,,1,,,,,,0) prints single pages
oFile.PrintOut(,,,,,,,1,,,,,,1) prints all pages as manuel duplex rather than true double sided
oWord.DisplayAlerts := -1 ; turns them back on
oFile.Close ; close the file
oFile := "" ; release the reference
oWord.Quit  ; close the application
oWord := "" ; release the reference
return
regards
J

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: scriptor2016 and 294 guests