Is it possible to write vba code to recognize the number of pages to be printed. If more than one page turn the duplex feature of the printer on and print.
Thanks,
John
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Duplex Printing (Eng/XL97/SR2)
John,
This seems a bit obvious, but why not set the printer on duplex all the time? Won’t this do what you want?
On the other hand I presume that you realize you will still have problems printing multiple sheets on duplex, as Excel always starts the next sheet as page 1, and hence on a new sheet. The only way I know around this would be to copy data to a separate printing sheet and print it all from there.
Your original query about vba code, I think, requires that the specific printer driver be known, as specifying duplex is a component of the printer driver, not Excel.
Hope this Helps.
Good Luck!
I don’t know if this would be any help, but in the course of asking a question elsewhere on this board I came across something you might be able to use … the {PRINT} field code can be used to switch duplex printing on and off – if you are using an HP printer, apparently you can put {print 27″&l#”} (use Ctrl-F9 to create the curly brackets, then type the rest yourself), where # is 0=duplex off, 1=duplex, long edge binding and 2=duplex, short edge binding. For more details, see Q135569, as Andrew Connolly kindly pointed out to me.
Unfortunately I still don’t know how to use these in a macro, but I’m hoping either I’ll work it out or someone will help me to do so!
HI John
Our company – in MS-Word uses print code to print.
thats all printing sent from word uses print code.
The standard printer hardware & models we use are lexmark printers, HP’s & other adhoc printers.
Your question is ‘can you print duplexing via code.’
Yes you can.
1stly the printer has to support duplexing.
My next project is to print duplexing via code.
I havent yet looked into or spent a great deal of time researching
but when I do are you interested as I can hopefully assist you & can you wait until then?
regards Diana
Diana,
Thank you for responding. The printer that I use supports duplexing. I am familiar with the old dot matrix printer setup strings that allowed the user this type of flexibilty in reporting (printing) but unable to find docmumentation for VBA code.
Any assistance you can provide is truly appreciate. I understand that your next project will focus on this and I am willing to wait.
Thanks again,
John
John,
Is this what you are looking for?
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
“PRINT 27″”&l2X”””, PreserveFormatting:=False
That field inserted into your document will download a PCL 5 printer command to print 2 copies of the document.
The command to switch the printer to Duplex mode is:
Text:=”Print 27″”&l1S”””
Back to Simplex is:
Text:=”Print 27″”&l0S”””
0 = Simplex
1 = Duplex, Long-Edge Binding
2 = Duplex, Short-Edge Binding
This info is taken from the HP PCL 5 Printer Language Technical Reference Manual.
John,
By using the old Macro4 language, you can get the number of print pages in a workbook. The following is the syntax you probably want :
If ExecuteExcel4Macro("Get.Document(50)") > 1 Then ' Code to set Duplex mode End if
It would help to set up a second instance of your printer with the default mode set to Duplex (assuming the normal mode is Simplex) and give it a different name. So you would then have for example “MyStandard Printer” and “MyDuplex Printer”. When using these names in VBA you need to include the name of the port the printer is attached to, for example the standard printer could be referred to as “MyStandard Printer on LPT1:”. The best way to get correct reference is to record a macro where you select the printer. Having done that you could then use something like :
Dim strPrinter As String strPrinter = Application.ActivePrinter If ExecuteExcel4Macro("Get.Document(50)") > 1 Then Application.ActivePrinter = "MyDuplex Printer on LPT1:" Else Application.ActivePrinter = "MyStandard Printer on LPT1:" End If ActiveSheet.PrintOut ' print the active sheet Application.ActivePrinter = strPrinter ' reset the default printer
to do what you want.
Andrew C
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.