1. Technology

Your suggestion is on its way!

An email with a link to:

http://visualbasic.about.com/library/courses/blecvbai0303.htm

was emailed to:

Thanks for sharing About.com with others!

Your First 'Job Worthy' Program
3 - The Form Letter Macro
 Lesson 3
Your First 'Job Worthy' Program

1 - Analysis - Plan Your Work

2 - Put Your Assistant To Work Again

3 - The Form Letter Macro

4 - Help!

5 - Take It One Step At A Time!


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 template rather than the AboutVB3.doc document. When you open the macro in the Visual Basic Editor (click HERE for the previous lesson where we did this), you see quite a bit more code than we had in our Hello World program. (Some minor reformatting has been done to keep the lines a little shorter here. If your macro doesn't look like this, it's probably because you left a step out when you were recording it.)

Sub AboutFormLetter()
'
' AboutFormLetter Macro
' Macro recorded 12/7/1941 by Dan Mabbutt
'
    Documents.Open _
        FileName:="AboutVB3.doc", _
        ConfirmConversions:=False, _
        ReadOnly:=False, _
        AddToRecentFiles:=False, _
        PasswordDocument:="", _
        PasswordTemplate:="", _
        Revert:=False, _
        WritePasswordDocument:="", _
        WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto
    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
    With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseEnd
        Else
            .Collapse Direction:=wdCollapseStart
        End If
        .Find.Execute
    End With
    With Selection.Find
        .Text = "(2)"
        .Replacement.Text = _
            "the Great Novel of Our Time"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseEnd
        Else
            .Collapse Direction:=wdCollapseStart
        End If
        .Find.Execute
    End With
    ActiveDocument.SaveAs _
        FileName:="AboutVB3_01.doc", _
        FileFormat:=wdFormatDocument, _
        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. 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 template. (You might have saved it in a different document instead of the Normal template. That's OK. Just follow these instructions for your document instead.)

In order to do this, we have to work with the Project Explorer again. This was introduced in the first lesson HERE. Note that there is no NewMacros module for the AboutVB3.doc document. 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 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.

Insert a Module

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. There! Now the code is stored in the document again. At this point, you might want to delete the AboutFormLetter subroutine or maybe even the entire NewMacros module from the Normal template. 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 in the next section:

4 - Help!

©2014 About.com. All rights reserved.