Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Temporarily Changing the Printer in a Macro.
Written by Allen Wyatt (last updated January 24, 2020)
This tip applies to Word 97, 2000, 2002, and 2003
Shaune has written a macro that prints, to a PDF writer, product information sheets. When he prints to the PDF writer, the macro resets the default printer for the system on which it is running, so that when someone then tries to print regularly (after the macro is finished), Word still assumes the user wants to print to the PDF writer. Shaune is looking for a way for his macro to use the PDF writer temporarily, without resetting the default printer.
The bad news, Shaune, is that you cannot get around resetting the printer. When you reset the ActivePrinter property, then Word assumes that the printer you specify remains the active (default) printer until you specifically reset it.
The good news is that you can also read the ActivePrinter property, which means your macro can save whatever printer the person had selected before the macro was run, and then reset the ActivePrinter property prior to exiting. The result is that the user never notices that the default printer was changed, because your macro sensed the settings and then changed it back. This technique is used in this manner:
Sub PrinterTechnique() Dim sCurrentPrinter as String Dim sPDFwriter as String ' perform whatever tasks need to be done prior to printing ' make sure you also initialize the sPDFwriter string with ' the name of your PDF writer ' save current printer name, then change to PDF writer sCurrentPrinter = Application.ActivePrinter Application.ActivePrinter = sPDFwriter ' print document, then go back to the original printer ActiveDocument.PrintOut Application.ActivePrinter = sCurrentPrinter ' continue with the rest of your macro End Sub
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (318) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Temporarily Changing the Printer in a Macro.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Add-ons can extend the capabilities of Word, sometimes significantly. These extensions are due to macros contained in the ...
Discover MoreIf you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros ...
Discover MoreIf you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-03-18 00:32:13
Malcolm Sokol
Just read the tip about temporary change to default printer to a PDF printer. This was for MS Word. My question is whether similar code works for Excel. Specifically, I would like to change the default printer so that a Userform.PrintForm command which goes to the default printer without a printer diaglog would work. Thanks. I am subscribed to your Excel tips but this tip came up doing a web search.
2016-05-18 17:46:01
CK
Here's the problem I have... When you reset to the string of whatever the original default printer was, you're not TRULY resetting to "default printer," you're specifically assigning it to a printer that, for the moment, happens to be the default printer. The problem comes in when a user changes their default printer OUTSIDE of VBA.
So say I've got a default network printer, we'll call it NetPrintA, and another printer called PdfPrint. I use VBA to grab the NetPrintA string as current printer, set activeprinter to PdfPrint, then later use VBA to set activeprinter to NetPrintA. NOW, let's say NetPrintA gets replaced with a brand new printer called NetPrintB, and in my computer's settings, I set the default printer to NetPrintB. Guess what? the activeprinter property still reads the current printer as NetPrintA.
So is there a way to use VBA to determine what the actual default printer is on your computer, rather than simply whatever is currently set to the activeprinter property?
2015-05-13 05:29:40
anon
for a network printer simply use \servernameprintersharename when setting the string value.
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments