You are on page 1of 2

Macro of the month

This month’s Word macro creates an envelope for you. The macro first displays a dialog
box into which you type up to four lines of the recipient’s address. When you click the
Make an Envelope button in the dialog, the dialog passes the recipient’s address to the
macro which displayed the dialog by way of a public variable. The code then creates a
new document with two text boxes – one contains your return address and the other
contains the recipient’s address.

You can customize this macro by adding your own return address into the macro code
where indicated. The macro creates a DL envelope for you and you can change this if
desired. Features to look out for in the macro include the fact that the text boxes on the
user form have been given tab index values so you progress logically through the form
and so you can press Enter to move from one text box to the next. Notice too that the
form returns a value to the calling macro indicating whether the user pressed Cancel or
not. If the user pressed Cancel, the code that creates the new envelope is skipped so that
nothing is created. You will find this macro in the document makeanenvelope.doc on the
cover disc together with instructions for customising it.

This is the code of the macro:

Sub newEnvelope()
usercancel = False
envelopeInfoForm.Show
Dim myaddress As String
crlf = Chr$(13)

myaddress = "Line 1" + crlf + "Line 2" + crlf + "Line 3" + crlf + "Line 4"

If Not (usercancel) Then


Dim makeNewDoc As Document
Dim makeNewTextbox As Shape

Set makeNewDoc = Documents.Add

With ActiveDocument.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1.25)
.BottomMargin = InchesToPoints(1.25)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.66)
.PageHeight = InchesToPoints(4.33)
.VerticalAlignment = wdAlignVerticalTop
End With

Set makeNewTextbox = makeNewDoc.Shapes.AddTextbox _


(Orientation:=msoTextOrientationHorizontal, _
Left:=150, Top:=150, Width:=300, Height:=120)

makeNewTextbox.TextFrame.TextRange = recipientAddress

Set makeNewTextbox = makeNewDoc.Shapes.AddTextbox _


(Orientation:=msoTextOrientationHorizontal, _
Left:=25, Top:=25, Width:=200, Height:=80)

makeNewTextbox.TextFrame.TextRange = myaddress
End If

End Sub

To make this your own, substitute your address for the entries in this line:

myaddress = "Line 1" + crlf + "Line 2" + crlf + "Line 3" + crlf + "Line 4"

Replace “Line 1” with the first line one of your address, “Line 2” with line two of your
address and so on. Leave the crlf code in place as this adds a carriage return between each
line ensuring that the address reads correctly. Make sure to use plain double quotes
around each text line.

If you want to create a different size envelope, change the .pagewidth and .pageheight
values to appropriate values for the envelope you want to create.

To add the macro from this document to your normal.dot file, first display the Visual
Basic editor by choosing Tools > Macro > Visual Basic Editor or access it through the
Developer tab in Word 2007 or 2010. Locate the envelopeinfoform and
envelopeinfomacro modules and copy them to your normal.dot file. You can then create a
toolbar button to run the macro for you.

You might also like