Section 19.4. Reading Visual Basic Code


19.4. Reading Visual Basic Code

When you record a macro, Word writes the actions and commands. In effect, it's writing a Visual Basic program for you. You can view the program using the Macro box. Simply choose Developer Code Macro or go to Alt+L, PM.

In the Macros box, click SentenceStrikeThruBlue, and then click Edit to see the code for the macro you recorded earlier in this chapter. A new window opens with what looks like an entirely new program (Figure 19-12). You're delving into the mysteries of Microsoft Visual Basic.

Figure 19-12. In the Macro box, select the name of your macro, and then click the Edit button at right to launch the Visual Basic editing program. The macro code is in the window at the right. This code is for the SentenceStrikeThruBlue macro.



Note: There's much more to Visual Basic than can be included in this chapter. In fact, you can find entire books on the subject. To learn more about Visual Basic for Applications, check out VB and VBA in a Nutshell or Learning Visual Basic .NET .

Even if you've never programmed in your life, you can see some familiar English words in Visual Basic code. In this example, the first line begins with Sub , and the last line is End Sub . Each recorded macro is called a subroutine in Visual Basic lingo, and these two lines mark the beginning and end of the SentenceStrikeThruBlue macro.

Here's how to decipher the rest of the code:

  • The lines beginning with a ' mark are comments; they don't do anything. They're just used to create blank lines as a way to include informative text and comments. In these comments, you see your macro's name and the description you typed when you created it.

  • Moving down, you see three lines with the same words: Selection.Extend . You created these lines when you pressed the F8 key, which Word refers to as the Extend Selection command.

  • The next line is pretty easy to interpret. Selection.Font. Color changes the font color for the selected text. The weird number after the equals sign is Word's term for the color of blue you chose.

  • The following line, Selection.Font. StrikeThrough , isn't hard to understand either, but the word after the equals sign is a little curious . It reads wdToggle . If you're guessing that this command toggles the strikethrough effect on and off, you're right. You deserve an honorary programmer's pocket protector.

  • The final line before End Sub reads Selection.MoveRight Unit:=wdCharacter, Count:=1 . Word recorded this code when you used the right arrow key to move the insertion point and deselect the sentence .

When you're done exploring, choose File "Close and Return to Microsoft Word to close the Visual Basic window. If you made changes to the code, you see a dialog box asking whether you want to save the changes.

19.4.1. Getting Help for Visual Basic

As described in the previous section, you can read the Visual Basic code for any macro you've recorded. And because Visual Basic uses relatively down-to-earth language, you can interpret many of the lines and figure out what they do. If you're not sure how a particular line of code works or what changes you can make, try using Visual Basic's Help system. It's very detailed and written for programmers, but if you're interested in learning how to tweak your macros after you record them, the help screens have the details you need.

To get help for the details of the Visual Basic code for the SentenceStrikeThruBlue macro, open the now-familiar Macros box (Developer Code Macros or Alt+L, PM). Select the macros name (SentenceStrikeThruBlue), and then click Edit.

The Visual Basic window opens, showing the macro's code in the right box. In the second to last line (Selection.Font.StrikeThrough = wdToggle) , click anywhere in the word StrikeThrough , and then press F1 to open the Visual Basic Help window (Figure 19-13).

You can get more information on any Word command by placing the insertion point in the command and pressing F1. In general, Visual Basic Help is much smarter than Word's Help when it comes to knowing what reference you need. Typically, you see the name of the command or property at the top of the Help box and below a description of the command in programmer's computerese. In addition, you usually find remarks and examples.

In this case, the most helpful text for the StrikeThrough command is the second sentence under Remarks, which explains that StrikeThrough can have three settings: True, False, and wdToggle. True adds the strikethrough effect to selected text, False turns off the strikethrough text, and wdToggle switches the strikethrough effect on and off. The second example shows how to apply strikethrough formatting using the word "True."

19.4.2. Making Simple Changes to Visual Basic Code

Armed with the knowledge provided by Visual Basic Help in the previous section, you know that in Visual Basic the strikethrough effect has three settings: True, False, and wdToggle. When you tested the SentenceStrikeThruBlue macro, you saw that it toggled the strikethrough effect on and off (Section 19.3). It would be better for the macro to always turn it on (or leave it on, if the text is already struck through).

Figure 19-13. The Help box for Visual Basic is just like Word's Help box. On the left, click a topic, and then you see an explanation in the large window on the right.


In this tutorial, you tweak the code in your macro. (Now's a good time for another sip of that Jolt cola.) Here's how to open your macro in Visual Basic, and then edit the code so that the macro always turns on the strikethrough effect.

  1. In the Macros box (Developer Code Macros or Alt+L, PM), click the macros name (SentenceStrikeThruBlue), and then click Edit .

    The Visual Basic window opens, with the macro's code displayed in the box on the right (Figure 19-14).

  2. Find the line with the StrikeThrough command, Selection.Font.StrikeThrough = wdToggle .

    The way it was recorded, your macro toggles the strikethrough effect on and off. You want to change it so it always turns on the strikethrough effect.

  3. Double-click the word wdToggle to select it, and then type the word True .

    Now the line reads, Selection.Font.StrikeThrough = True . As you read in the Visual Basic Help window, True is one of the options for the StrikeThrough command. It sets the strikethrough effect to "on."

  4. In the menu bar, choose File Close and Return to Microsoft Word .

    The Visual Basic window closes , and you see your document.

    Figure 19-14. You see a lot going on in the Visual Basic editing window, but there's no need to be overwhelmed. To make simple changes, just focus on your macro's code in the window on the right. This figure shows wdToggle selected and ready to be replaced with the word True.


You can test out your Visual Basic editing to see if it works as planned. Click a sentence, and then run your macro with the Quick Access toolbar button or the keyboard shortcut Ctrl+Alt+Shift+X. The sentence turns blue, and you see the strikethrough effect. Click the sentence again and run the macro again. Instead of removing the strikethrough as it did in the original macro, the text should stay the sameblue with a strikethrough.

POWER USERS' CLINIC
More Visual Basic Tweaks

If you thought the previous example was fun, you can try making some more changes to the SentenceStrikeThruBlue macro. For example, you can change the color that's applied to the text by changing the line that begins Selection.Font.Color =. Visual Basic can use numbers or special words to identify colors. For humans , the words are easier. Try changing the number after the equals sign to one of these words: wdColorRed, wdColorDarkGreen , or wdColorYellow . The line should look something like this:

 Selection.Font.Color = wdColorRed 

In this way, you can change the color the macro changes text to, without re-recording the whole macro.

Poke around and see what else you can change. For example, try changing the number at the end of the line that reads:

 Selection.MoveRight Unit:=wdCharacter, Count:=1 

Try changing the word Selection.MoveRight to Selection.MoveLeft or Selection.MoveUp . All these commands deselect the text that the macro selected at the beginning, but they leave the insertion point in a different place.




Word 2007[c] The Missing Manual
Word 2007[c] The Missing Manual
ISBN: 059652739X
EAN: N/A
Year: 2006
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net