Microsoft Word offers a bunch of fields that you can use to do some
really cool stuff. If you wanted to have the name of a document automatically
inserted into a document, you would add the FILENAME field. If you wanted table
cells automatically numbered, you could enter the AUTONUM field. This article
will concentrate on the MACROBUTTON field.
With this field, you can run a macro (a set of recorded or programmed commands)
right from within a document. It's a great way to help out users who don't understand
Word very well. Rather than telling them to click here and click there, you
can just display words or an image right inside the document and have them click
it to run whatever commands you need, by running a macro that contains your
preprogrammed commands.
This is the first of two articles. In this article I'll show you how to set
up a master letter template form, code a macro for envelope printing and add
a macro button field to your template to allow you to double click the field
and have the address in your letter automatically printed to an envelope. Next
month, I'll show you how to add an image to the macro button, such as a picture
of an envelope or command button, as well as show you a trick with VBA code
to turn the macro button's default double click into a single click field.
Fields
There are many fields you can use in Word. And Word's Help files provide
all the info you need to learn about each field. The only problem is that there's
a bit of a trick to finding the help info you need. If you highlight
a field in the Insert/Field dialog box and hit F1 for help, either your Office
Assistant will jump out and ask you what you want or you'll get the help dialog
telling how to get help in Word! So here's a little trick to make it easy to
find what you need.
Click Insert/Field to open the Field dialog box. Find the field
you want by either opening one of the categories in the left column of
the dialog box, or by choosing All from the left and scrolling
down the full list in the right column to find the one you want. When
you find the field you want details about, click once to highlight it.
Once highlighted, click the little question mark button [?] in the upper/right
of the dialog. This is your Help button. Now go click on the highlighted
field again.

This will bring up the correct Help dialog providing you with all the info
you need to learn about that particular field.

How Efficient Are You?
The macro button field will run any macro by double clicking on whatever image
or words you've added to the field to represent it on your page. Say you recorded
a macro of the steps you take to print an envelope. Now whenever you need to
print an envelope you can highlight the address in the letter, click Tools/Macro/Macro,
find that envelope macro, highlight it and click Run to have the highlighted
address inserted into the Envelope dialog. Then you can click Print.
But then, wouldn't it be just as easy to highlight the address and click Tools/Envelopes
and Labels/Envelope and click Print? Since efficiency for using software
is judged by how quickly you can perform any task in a given program, both of
these steps are about the same amount of clicks. They'd both take you about
the same amount of time to perform, so. Why bother with a macro?
A shorter way to do it would be to add the Envelope shortcut icon
to a toolbar or custom menu. Then you can just highlight the address in your
letter and click the envelope shortcut. This will bring up the envelope dialog
box with the highlighted address inserted so you only need to click Print
to print the envelope. Not bad. But I even have trouble remembering where I
put shortcuts or icons sometimes, or maybe I've added so many custom icons to
my main toolbar that many are now hidden off the screen which means I have to
hunt for it. That's still not as efficient as having a print command right in
my letter template!
Note! To learn how to add the envelope shortcut to a toolbar, see this
article: Letter
Wizard. And to learn how to customize your toolbars and menus, see this
article: Customizing
Word Toolbars.
I'm going to teach you how to write a little code and set up your template
so you can print an address with a double click!
Create a Master Letter Template
The first thing you'll need is a master letter template with a form field inserted
where you'll enter the recipient's address. You must at least use this one field,
so you can tell the macro where to find the address in the letter.
Open a blank template in Word by clicking File/New and checking the
Template option in the lower/right portion of the dialog box.
Start by adding a date field, so you'll always have the current date on your
letter. Click Insert/Field/CreateDate. This will automatically provide
the current date to each new letter you create from this master template. Be
sure to click Options on the Field dialog box and adjust the date format
to the one you want.
Setup your master letterhead in whatever way you want. Then move to the location
where you'll be entering the Recipient's Address. Click View/Toolbars
and turn on the Forms Toolbar. Where you want the recipient address,
insert a Text form field from the Forms Toolbar. If you're not sure which
one that is, hover your mouse over the first button and you'll see the tooltip
tell you which one is the Text field. Click to enter it into your template.
You can also add one for the Salutation and Letter text, if you want. Your template
should now look like the image below.
Note! Our blind readers should know that you can click Tools/Customize/Keyboard.
In the left Categories command select All Commands. In the right
Commands list select Text Form Field and you can then assign the
Insert Text Form Field command to a keystroke combination. If you add
it to a menu, it will appear on the menu as Edit Box, so the hotkey will
be Alt/E.

To have your screen look like the image above, you need to make sure you have
your fields shaded and your bookmarks turned on. So click Tools/Options/View
and check the Fields shading, then also check the Bookmarks on
so you can see the brackets that represent the bookmarks in the field.

Double click in the recipient form field in your template to open its
Options dialog box (or right click the field and choose Properties).
There you should give the field a proper bookmark name. Something more recognizable
than Text1, which will be the default. I'm using bkRecipient as
you can see in the image below. You can also add default text. I've added Type
recipient name here! to remind me what needs to be typed in this field.

When you close this dialog, you should see the field(s) displayed as in the
image below.

To make the form field work, you need to Lock your template by clicking
Tools/Protect Document/Forms.
Now when you want to write a letter, you can click File/New, retrieve
your template form and start typing your letter, by hitting the Tab button to
jump from field to field, entering the needed information as you go.
Record a Macro
Now we'll record an envelope macro and write a little VBA code to make it work.
Don't worry if you've never written any code beforeI'll show you what
to do. Then we'll use the MACROBUTTON field to insert our macro into the master
letter template. From then on, whenever you have a letter and need to print
an envelope, you'll just need to double click the macro button text that'll
be displayed right in your letter to have the address printed to an envelope.
We'll start the macro by recording some of it and then we'll go into the code
editor and enhance the code. Click Tool/Macro/Record Macro or just double
click the tiny REC button at the bottom/middle of your Word window.
This will open the Record Macro dialog box. Give your macro a name.
Note! Be sure you click the Store macro in: drop down
and choose your master template. This way, the macro will travel with
your master template should you move it or pass it to a colleague. If you put
the macro in your Normal.dot (default template), there's a good chance it won't
work in the future!

After you give it a name and set the template where the macro should be stored,
click OK. The Macro Recorder toolbar will appear and all your
steps will now be recorded, so try not to make a mistake or it too will be recorded!

Click Tools/Envelopes and Labels/Envelope in Word 2000. In Word 2002,
click Tools/Letters and Mailing/Envelopes and Labels/Envelopes. Make
sure all the Options are properly set for the envelope size and letter
feed position. These settings will be recorded into the macro, so make sure
you have the settings you'll always want to use.

And also be sure that you've selected Manual Feed for the print tray.
This way you won't accidentally print your envelope on a sheet of paper. The
printer will go into manual mode and wait for you to come feed in an envelope.
Just remember to go feed it, so you're not holding up other print jobs. If you
have an automatic envelope feed tray, select that tray.

Once you're sure everything is set the way you'll need it, click Print.
Realize that this will cause a blank envelope to print. Go feed one in so it'll
resume regular mode.
Now stop the macro recorder by either clicking the square stop button
on the recorder toolbar or clicking Tools/Macro/Stop Recording.
Modifying the VBA Code
Now we need to go into the Visual Basic Editor (VBE) to make a few modifications
to the code so it'll work correctly. Click Tools/Macro/Macro. Select
the name of the macro you just recorded and click Edit. You can also
hit Alt/F11 to enter the VBE, but if you're not familiar with it, it'll
be easier to go in this way so you'll instantly be located within the macro
code you need to editwhich will be the NewMacros module within the template
project you're creating.
You should see VBA code similar to mine below.

We need to add a little code. The name of my macro sub procedure is
called Sub EnvelopePrint(). After that line, I will hit enter and type the following
code. You do the same and type it exactly as I've typed it.
Dim myAddress As String
myAddress = ActiveDocument.FormFields("bkRecipient").Result
If you're not familiar with VBA code, not to worry. As long as you've typed
it the exact same, it'll work. Just realize that the "bkRecipient"
name is the name of my form field bookmark that I previously entered
into the form field options dialog. If you used a different name, add that same
name here between the quotes.
The first line, Dim myAddress As String, declares or dimensions
your variable. In other words, it tells your computer that you will be passing
it some information as a string of text so it should put a small portion of
memory space on the side to be ready for this information when it arrives.
The next line, myAddress = ActiveDocument.FormFields("bkRecipient").Result,
sets the variable. It tells Word, ok...my variable (the virtual bucket that
will hold the info I send it) will be called myAddress and I want you
to put into this variable, whatever information you eventually find (when the
code runs) that will be sitting in the results of the form field called bkRecipient.
In other words, it sets the variable equal to the bkRecipient form field results.
That way, when you run the code, whatever address is typed into that form field
will be extracted into this variable in your computers temporary memory and
will be inserted into the envelope's address field when the envelope is printed.
But before that happens, you need to put the variable in the address location
of the recorded code. Look for the portion of the envelope code that says: Address:="",
and delete the two current quotes and replace them with the name of your
variable. The two current quotes tell the address to print nothing! Because
there is nothing between the quotes. If you did have some information
in the envelope dialog when you recorded the macro, there may be some info in
there. But you don't want anything in there because you will be telling the
code to use the information from your variable as the address. And if
you remember, the information in the variable will eventually be whatever is
typed into the bkRecipient form field when you run this code. So change
the Address:="", code to read Address:=myAddress, by
replacing the quotes with the name of your variable.
There is one final problem we need to work around with some new code. The template
must be locked (Protected) to get the form fields to work when you tab into
them. But if the form is locked, you won't be able to click the MACROBUTTON
form field we'll be putting in the template. So will soon be adding a section
break into the template. This will lock part of the page, but leave part of
it open. However, the envelope code we recorded won't work properly from the
protected code, so we need to write a quick routine that will instantly unlock
the form and then instantly relock it after the envelope command is activated.
After the last line of your Envelope macro, hit the Enter key and type
the following new code, exactly as I have.
Note! If you will not be using a Password, then do not include
the blue text in the code below, which begins with the word Password in the
Unprotect code line and begins with the comma after the word True in the Protect
code line. Also, be sure to enter the name of your password between the
quotes where I now have DiansPassword typed.
Sub ToggleDocProtection()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields
Then
ActiveDocument.Unprotect
Password:="DiansPassword"
Else
ActiveDocument.Sections(1).ProtectedForForms
= False
ActiveDocument.Sections(2).ProtectedForForms
= True
ActiveDocument.Protect
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="DiansPassword"
End If
End Sub
And finally, you'll add the call to this routine within your previous
macro so it Toggles the protection on/off before and after the envelope code.
In other words, the original macro will set up the variable. Then it will quickly
call the Toggle code which unlocks the form, runs the envelope and quickly locks
it again on the second call, all in a nanosecond. The final code should look
like this:
Sub EnvelopePrint()
Dim myAddress As String
myAddress = ActiveDocument.FormFields("bkRecipient").Result
ToggleDocProtection
ActiveDocument.Envelope.PrintOut ExtractAddress:=False, OmitReturnAddress
_
:=False, PrintBarCode:=False, PrintFIMA:=False,
Height:=InchesToPoints( _
4.13), Width:=InchesToPoints(9.5), Address:=myAddress,
AutoText:= _
"ToolsCreateLabels3", ReturnAddress:="MouseTrax
Computing Solutions", _
ReturnAutoText:="ToolsCreateLabels4",
AddressFromLeft:=wdAutoPosition, _
AddressFromTop:=wdAutoPosition, ReturnAddressFromLeft:=wdAutoPosition,
_
ReturnAddressFromTop:=wdAutoPosition, DefaultOrientation:=wdLeftLandscape
_
, DefaultFaceUp:=True
ToggleDocProtection
End Sub
________________________________________
Sub ToggleDocProtection()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields
Then
ActiveDocument.Unprotect
Password:="DiansPassword"
Else
ActiveDocument.Sections(1).ProtectedForForms
= False
ActiveDocument.Sections(2).ProtectedForForms
= True
ActiveDocument.Protect
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="DiansPassword"
End If
End Sub
Realize that some of the original envelope code you recorded may be slightly
different depending on your system. But make sure all the code we manually typed
in is the same, except for any bookmark or password name modifications.
Press Ctrl/S to save the code and close the VBE to return to your template.
The MACROBUTTON Field
You have your macro finished, now you need to add the MACROBUTTON field. In
this lesson, I'll show you how to add it by default. Next month in part two
of this lesson, I'll show you how to enhance it by adding an image and changing
the click default to one click, with a little extra VBA code.
Open your master template (if it's not still open). Do not open it by
using File/New; you must open the file with File/Open and then go find
it. If you double click it or open it via the File/New dialog, you will not
be opening the master template! You'll be creating a document from the
letter template. Therefore, none of the changes you do to it will be saved to
the master template. So when you open the file, check the file name in the title
bar of Word to make sure it says dot and not doc.
Place your cursor at some out of the way location, such as near the top and
click Insert/Field. Locate the MACROBUTTON field and click it.
Then click Options in the Field dialog. Locate your macro in the macro
list and click Add to Field. Then add some text as the display text
that will show up in the field. Be sure to wrap the display text in quotes.
See the image below for details.

Click OK to back out of the Field dialog box. You'll then see that you
have a field in your letter that had the words you typed into the field displayed.

If you highlight the field and hit Shift/F9, you'll convert the text
into its code and you'll see the details that you entered into the Field dialog
which are now hidden under the display text.

And speaking of hidden, since you don't want the macro button display
text showing on your printed letter, highlight the code and click Format/Font
and choose Hidden from the options at the bottom of the Font
dialog box, or click Ctrl/Shift/H to apply hidden formatting to the code.
This may cause your code to disappear if you don't have the option to View
hidden text turned on. If so, click Tools/Options/View and check
the Hidden Text option so you'll be able to see it on your screen. You
should now see the display text with tiny dots under it. Those tiny dots
signify that the text is hidden.
However, you'll also have to make sure that you don't have the option to Print
hidden text turned on or it'll still print! So click Tools/Options/Print
and make sure the Hidden Text option on that dialog is not checked.
One last step for this part of the lesson. Place your cursor just after the
field code and click Insert/Break/Continuous to enter a section break.
Remember in the toggle procedure we wrote, it said to set the protection
in section one to False, but the protection in section two as True. This will
lock the form so the fields will work, but leave the macro code unprotected
so you'll be able to double click to access it and run the code.
You'll need to click the Show/Hide button to see the Section Break.
In the image below, you'll see the page with the section code displayed and
you'll see Rocky is pointing to the Show/Hide icon, which looks like
a backward P.

And that's it for the basics! You can now resave your master template. When
you need to type a letter, click File/New and retrieve your master template
to create your letter document. After you've typed your letter, double click
this macro button code and your macro will access the address you typed into
the form field, pass it to the variable in the code and print your envelope
with that address on it. Just remember to click Tools/Protect Document and
manually protect it for forms only in section two.
The follow-up article to this one is now available. See the Enhancing the MACROBUTTON Field article from the September issue of TechTrax to learn how to add an image to your envelope macro button and change the activation to a single click.
Need more help getting your VBA code working right? Join our free VBA support
groups. See these links for details: http://groups.yahoo.com/group/Word_VBA/ and/or
http://groups.yahoo.com/group/ExcelVBA/.
|