Thursday, February 20, 2014

A MACRO TO PRINT ENVELOPES WITH POSTNET BARCODES

A while back, a colleague of mine asked how they could print envelopes with the POSTNET barcode on them. It turns out that Microsoft Word doesn't make this a simple process. So, I decided to write a macro to do this for them. I thought I would share it.

I've pasted the macro VBA code in blue below for you to copy into your own macro module. To use the macro, highlight the recipient's address in your document and run the macro. It will then ask you to type in the zip code. It will then print both your letter and an envelope with the POSTNET barcode on it.

As a note, please remember that I am an attorney and am entirely self-trained when it comes to programming. So if you see the code and cringe, I'd welcome any feedback on how it could be improved.

Sub BarCode()

'
' Joshua Goodwin
' 4/16/2012
' Adds an envelope and POSTNET barcode to document


'  Define and fill Variables

    Dim strAddress
    Dim strZipCode As String

    strAddress = Selection.Text
                  
    strZipCode = InputBox("What is the Zip Code?")

' print letter

 Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
        wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
        PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    
 ' Create envelope and add to document
    
    ActiveDocument.Envelope.Insert Address:=strAddress & "**"
        
 ' Find location to insert barcode
    Selection.MoveUntil cset:="**"
    
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeBackspace
    Selection.TypeBackspace
    
 ' insert barcode

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "BARCODE  \u" & strZipCode, PreserveFormatting:=True
    
 ' Print envelope
    
    ActiveDocument.Envelope.PrintOut
  
 ' Delete envelope from document
  
    ActiveDocument.Undo (4)
    


End Sub

1 comment:

  1. Your encounter while in helping learners creates a person a good respected institution though The idea come in order to giving learners at what to write down powerful entry documents.

    ReplyDelete