Word Tutorials With VB

Microsoft Word has a powerful scripting language called Visual Basic, which you can use to automate any task in Word. You can create Visual Basic scripts, called macros, by recording them, or by entering them manually in the Visual Basic IDE (integrated development environment) of Word.

One of the most common tasks that Word users need to do is find and replace text. While Word's existing functionality for find and replace is easy to use, Visual Basic can make it even easier. Enter and run the following macro to learn how to find and replace with Visual Basic.

Create the Program

    You'll create a short program by entering it directly in the Visual Basic IDE. But you first need a sample document to test the program with. Inside Word, open up a new document and save it with the name "vbReplace.doc." Copy a lot of text from another document and paste it into vbReplace.doc.

    Press alt-F11 to enter the Visual Basic IDE and expand the Project vbReplace subtree in the Project pane. Expand the Microsoft Word Objects folder and double-click the ThisDocument icon. Visual Basic places the cursor in the code entry window. Enter the following code in that window.

    Public Sub mac()
    Dim myRange As Range

    'do it with current paragraph
    Set myRange = Selection.Paragraphs(1).Range

    With myRange.Find
    ClearFormatting
    .Font.Bold = False
    .Format = True
    .Replacement.ClearFormatting
    .Replacement.Font.Bold = True
    .Execute Forward:=True, Replace:=wdReplaceAll, _
    FindText:="the", ReplaceWith:="the", MatchWholeWord:=True
    End With
    End Sub

Run the Program

    Now that the program is done, it's time to test it. Return to the Word document by pressing alt-F11 and place the cursor in any typical paragraph, especially one that has lots of instances of the word "the." Run your macro by pressing alt-F8 to get to the listing of macros, selecting "mac" from the list and pressing Run. If there were any un-bolded instances of The in the paragraph where you placed the cursor, they now are all bold.

Modify the Program

    Having one macro to do a single type of find and replace has some use, but being able to easily adapt the macro to cover different kinds of replace operations is a real productivity booster. Re-enter the Visual Basic IDE (alt-F11) and navigate back to the code you entered, using the Project pane as described earlier.

    Change the Mac sub so that it turns some of the "The" words you bolded into italicized "la"s. This time, you'll restrict the replace operation to work just on one sentence. Here are the only lines you need to change:

    Set myRange = Selection.Paragraphs(1).Range becomes Set myRange = Selection.Sentences(1)
    .Font.Bold = False becomes .Font.Bold = true
    .Replacement.Font.Bold = True becomes .Replacement.Font.italic = True
    .ReplaceWith:="the" becomes ReplaceWith:="la"

Run the Revised Program

    Now you'll test the program in the same way you tested the original version. Return to the Word document and place the cursor inside a sentence with some bold instances of "The." Run your revised macro using the steps you used to run the original version. Watch the bold "the"s turn into "la"s. Notice also that the current sentence was affected rather than the whole paragraph.

Blog Archive