Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Temporarily Changing the Printer in a Macro.
Written by Allen Wyatt (last updated June 7, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016
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, in this example, that the active printer is first saved in the sCurrentPrinter variable, and then the active printer is set to whatever is in the sPDFwriter variable. (As noted, you'll need to initialize sPDFwriter so it contains a valid printer name.) After the printing is done, then the active printer is set back to what you previously stored in the sCurrentPrinter variable.
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 (9808) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Temporarily Changing the Printer in a Macro.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you want to decrease the size of spaces in some selected text, the best approach is to use a macro. This tip includes ...
Discover MoreDeclaring variables in a macro is good programming practice. Here’s how to do it and how to make sure they are all ...
Discover MoreWhen creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-01-12 11:39:26
Church
Im using Word 2016 and this works perfectly but only in the VBA editor window. When I run it on a document, outside the editor, it gives me a blank error window. This screenshot shows the error with your code and my code in the editor: http://take.ms/UcvR2
any help is greatly appreciated.
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu 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