Results 1 to 1 of 1
2002-04-03, 21:31 #1
- Join Date
- Jan 2001
- Sun Prairie, Wisconsin, Wisconsin, USA
- Thanked 125 Times in 120 Posts
Default envelope printer - VBA kludge (97+)
A common question comes up when people have one printer that is used for envelopes and another that is used for everything else, they would like to be able to switch to that printer automatically. Can't be done using the Envelopes and Labels Wizard.
However, it is possible to kludge around it using VBA. What follow are two macros used for this purpose. Both go in a global template and the second is attached to a button on the Standard toolbar.
The first macro simply prints the current document to the printer selected as the envelope printer. Because Word 2000 resets the default printer for Windows when you change the active printer using VBA, this macro first saves the current printer name, then switches, prints, and resets the current printer. Unfortunately, if you have already changed the printer, you will have a new default printer for Windows.
The second macro prints an envelope with the selected text using the template "mtEnvelope.dot" which contains a bookmark "Address" (and uses the same bookmark in the barcode). That template is stored in the user templates folder in the subfolder "Letters & Faxes." The macro calls the macro I listed earlier to do the actual printing. (Again, I only want to be setting that printer in one place.) This macro is attached to a button on my standard toolbar.
This doesn't have any of the artificial intelligence of Word's wizard to find the address, it relies on the user selecting it.
' mtPrintEnvelope Macro
' Macro written 15 January 2001 by Charles Kyle Kenyon
' Prints current page on designated printer and resets Active Printer
' This macro goes in a global template and is the only place the printer
' is set. The macro is called from envelope templates in
' FilePrintDefault procedure.
' Macro written by first recording a macro
' that printed to the designated printer
Dim sMyActivePrinter As String
Selection.Collapse ' In case called by macrobutton field
sMyActivePrinter = ActivePrinter
ActivePrinter = "Canon BJC-240 Plus"
Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
ActivePrinter = sMyActivePrinter
' mtEnvelopePrintButton Macro
' Macro written 3 April 2002 by Charles Kyle Kenyon
' Prints envelope addressed to selected text on designated envelope printer
' Requires mtPrintEnvelope procedure
' Contains very rudimentary error handler
' Process for pasting from clipboard kludged from VBA help file
' Process for copying into bookmark and recreating it from MVP site
Dim sTemplatesPath As String ' location of envelope template
Dim sTemplateName As String ' name of envelope
Dim BMRange As Range
Dim MyData As DataObject
Dim sAddress As String
Set MyData = New DataObject
On Error GoTo EndOfProcedure ' Give up without generating error
Selection.Copy ' Put selected text in clipboard
If MyData.GetText(1) = "" Then ' no text selected!
MsgBox Prompt:="Please select (highlight) address for envelope.", _
Title:="No address selected!"
' Put data in variables
sAddress = MyData.GetText(1) ' Paste clipboard to variable
sTemplatesPath = Word.Options.DefaultFilePath(wdUserTemplatesPath) _
& "Letters & Faxes" ' Folder holding template
sTemplateName = "mtEnvelope.dot"
' Create new document based on envelope template
Documents.Add template:=sTemplatesPath _
& sTemplateName, Visible:=False
' Identify current Address Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("Address").Range
BMRange.Text = sAddress
' Re-insert the bookmark
ActiveDocument.Bookmarks.Add "Address", BMRange
' Print using envelope print macro
Application.Run MacroName:="mtPrintEnvelope" ' see earlier macro
' Close envelope without saving changes
The first macro is also called in my envelope templates by the following:
This intercepts the Print button on the Standard toolbar and prints to the envelope printer.
Thought I would toss this out in case anyone else could use it.Charles Kyle Kenyon