Using the Visual Basic editor under the Developer tab, open the macro created in the previous section and let's see what it looks like! Remember that you will find it in the Normal project. When you open the macro in the Visual Basic Editor, you see a lot more code than we had in our Hello World program.
Some minor reformatting has been done in the document below to keep the lines a little shorter for this article. If your macro doesn't look like this, it's probably because you did the steps slightly differently when you were recording it. Minor changes are OK because we'll be changing it anyway. A careful comparison with this macro can do a lot to help you understand what the macro recorder does.
Sub AboutFormLetter()
'
' AboutFormLetter Macro
'
Documents.Open FileName:="AboutVB3.docm", _
ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", _
Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, _
XMLTransform:=""
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(1)"
.Replacement.Text = "Mr. Publisher Dude"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
. . . <code not shown>
ActiveDocument.SaveAs FileName:="AboutVB3_01.docm", _
FileFormat:=wdFormatXMLDocumentMacroEnabled, _
LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", _
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
The first thing you may notice is that the macro recorder has a tendency to create code that is longer and more detailed than necessary. If you feel like it, you can delete a lot of the properties that are coded as "named parameters" (after the ":=" characters). In fact, experimenting with these is a great way to get to know VBA and the objects used. For example, the obscure ...
SaveAsAOCELetter:=False
... named parameter simply states that the document should not be saved in a format that is compatible with Windows CE for palmtop computers. This is not likely to be a problem for you and you can safely delete this named parameter. Or ... you can leave it as it is! Your choice!
One thing we will do, however, is correct the problem we had because we had to save it to the Normal project. (You might have saved it in a different document instead of Normal. That's OK. Just follow these instructions for your document instead.)
You probably have the AboutVB3_01.docm document open. If you do, close it and open the AboutVB3.docm. This is the document that will contain the macro.
Open the Visual Basic editor and notice that there is no NewMacros module under AboutVB3.docm in the Project Explorer pane. So we need to insert one.
A module is just a place to store VBA code for the document. And NewMacros is just a default name that Word uses.
Right click the project name (AboutVB3) to get a context menu (or use the Insert menu item) and select Insert and then Module. Word gives this module a default name of Module1.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Now open the code window for NewMacros again and select the entire AboutFormLetter subroutine and copy it. Then reopen the Module1 code window and paste the subroutine. When the document is saved, the code is stored in the document.
At this point, you might want to delete the AboutFormLetter subroutine or maybe even the entire NewMacros module from the Normal project. Be careful, however. Remember that the Normal template has code and settings for your entire Word configuration, not just this project. If there's anything else in the Normal template that you want to save, don't delete it by mistake.
The point here is that it's just this easy to create a code module. In this case, we 'copy n' pasted' our code into it, but you could have just started writing code in the window too. But what to write? One place to get inspiration is the Help system for VBA. And we'll consider that on the next page.

