Word 2007[c] The Missing Manual
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 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.
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:
When you're done exploring, choose File 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 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).
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.
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.
|