Chapter 48. Macros: Recording, Editing, and Using

IN THIS CHAPTER

  • Recording macros

  • Editing macros

  • Testing macros

  • Using SelfCert to create your own certificate for digitally signing macros

  • The top 10 things you need to know about Visual Basic

Word macros are programs that you use to automate things you do in Word. At their simplest, macros can automate repetitive chores. At their most complex, macros are full-blown VBA (Visual Basic for Applications) programs that add features to Word. You can use macros to create shortcuts to features that aren't directly available and assign them to the Quick Access Toolbar or to keyboard shortcuts. You also can use a macro to perform a series of editing steps.

Why are they called macros? Macro means big. In effect, a macro is a way of boiling a lot of instructions (something big) into a single command (something small).

When Word for Windows first came out, a long time ago in a galaxy far, far away, its macro language was something called WordBASIC. At the time, it was new, exciting, and powerful and was far easier to use than it is today, unfortunately. With that ease, however, came malice. It wasn't too terribly long before the macro virus was born, and life hasn't been as simple or as pleasant since.

As a result, much of the emphasis and coverage of macros necessarily deals with security, and how you ensure that only the macros you trust can frolic across your computer screen. In this chapter, you'll learn about recording macros, performing simple editing to enhance your macros, and using macros. You'll also learn about the aspects of macro security not already covered in Chapter 47.

Note

Providing a full grounding in macros, Visual Basic, and using the Visual Basic editor is beyond the scope of this book. If you're a Word developer, you very likely already know everything you'll read in this chapter. This chapter isn't for you. Instead, it's for users who want to know about how to get started recording and writing macros. Once you've gotten started, take a course or invest in a book devoted to Visual Basic.

Macro Tools

Macro tools are generally housed in the Developer ribbon, which is hidden by default. To make it available, choose Office

Macro Tools
The Code section of the Developer ribbon provides access to tools for recording and editing macros, and controlling macro security.

Figure 48.1. The Code section of the Developer ribbon provides access to tools for recording and editing macros, and controlling macro security.

It's also possible to do a fair amount of work with macros without using the Developer ribbon at all, assuming you're willing to learn keystrokes shown in Figure 48.1. To access macro recording without the Developer ribbon showing (for example, if a different ribbon tab is showing), enable the macro tool on Word's status bar, as shown in Figure 48.2.

Record macros using the status bar control.

Figure 48.2. Record macros using the status bar control.

Tools in the ribbon and status bar are as follows:

  • Record Macro—Use the Record button to begin recording a macro. Most keyboard and ribbon actions are recorded, but mouse selection and text manipulation are disabled.

  • Pause Recording—Pauses the macro recording so you can perform actions you don't want recorded

  • Macros/Play Macro—Opens the Macro dialog box so you can choose a macro to run, edit, create, delete, or step into

  • Visual Basic—Opens the Microsoft Visual Basic editor, providing full access to different macro projects

  • Macro Security—Opens the Macro Settings section of the Trust Center for setting macro security

Recording a Macro

To begin recording a macro, click the Record Macro button either in the Developer ribbon or on the status bar. This displays the Record Macro dialog box, shown in Figure 48.3.

Replace the default macro name with one that will help you identify it later.

Figure 48.3. Replace the default macro name with one that will help you identify it later.

In the Macro Name field, type a name for the macro. Note that macro names can contain letters, numbers, and the underscore character, but cannot contain spaces. By default, Word numbers new macros sequentially. However, those names are not terribly useful. Replace the Macro# with a name that indicates something about what the macro will do. In Description, type a succinct description of the macro's intended purpose.

Note

If you give a macro the name of a built-in Word command, your macro will replace the built-in command while the template or document in which the macro is stored is active. To see a list of built-in command names, click the Macros button in the Developer ribbon, and set Macros In to Word Commands. Of course, you probably don't want to do that right now if you're in the middle of recording a macro.

Use the drop-down arrow next to Store Macro In to set the storage location. Macros can be stored in templates or documents. The default location makes the macro available in all documents, and stores the macro in Normal.dotm. If you choose to store the macro in a .dotx or .docx file, ultimately, as discussed in Chapter 5, the containing file will need to be saved as a .dotm or .docm file. This is discussed later in this chapter.

From the Record Macro dialog box, so you can easily access and test your macro, assign it to run from either a Quick Access Toolbar button or a shortcut key. Ultimately, it could be assigned to both, but you can only do one or the other from the Record Macro dialog box. Hence, you have three options:

  • To run the macro from the Quick Access Toolbar, click Button. Word displays the dialog box for customizing the QAT. In the upper-right corner, use the drop-down arrow to set the storage location. In the left panel, click the only macro listed, and then click Add

    Replace the default macro name with one that will help you identify it later.
  • To assign the macro to run from a shortcut key, click Keyboard. In the Customize Keyboard dialog box, press the shortcut key(s) you want to use. Set Save Changes In to the appropriate location, and then click Assign

    Replace the default macro name with one that will help you identify it later.
  • Do neither of the above and click OK. To run the macro, you will need to click Play, select the macro from a list, and click Run.

After making an assignment or clicking OK, Word displays the Recording Macro pointer. In addition, on the status bar, the Record Macro tool changes from a red ball to a blue square, both shown in Figure 48.4 (without the color).

Word has two ways to indicate that macro recording is in progress.

Figure 48.4. Word has two ways to indicate that macro recording is in progress.

When recording is in progress, your mouse options are restricted. You can select commands from the QAT and ribbon using the mouse, but you cannot use the mouse to select text or perform other editing. For selection and other editing, you need to rely upon a combination of the keyboard, ribbon and QAT.

For this example, the macro is named ToggleBookmarks. Let's record a macro to toggle the display of bookmarks, both because there is no built-in command to do it and because doing it using the Word Options dialog box requires a number of steps.

While the recorder is recording, choose Office

Word has two ways to indicate that macro recording is in progress.

On the status bar, click the blue square, indicated in Figure 48.5, to stop the macro recorder. At this point, you have a macro that sets the current state of bookmark display—not very useful as such. To make it useful, you need to edit it to turn it into a toggle instead.

When macro recording is in progress, the status bar Record Macro button turns into a blue square.

Figure 48.5. When macro recording is in progress, the status bar Record Macro button turns into a blue square.

Ideally, when you record a macro, it will work without further intervention. Sometimes, however (as in the current example), you need to edit it to make it do what you want.

Editing a Macro

To edit the macro, click the Macros button in the Developer ribbon. This displays the Macros dialog box, shown in Figure 48.6. Select the macro you just recorded and click Edit. If you didn't store the macro in the default location (Normal.dotm), you might need to set Macros In to the correct location in order for your macro to appear in the list.

When you click Edit, Word opens the Visual Basic editor, as shown in Figure 48.7. The section on the left is called the Project Explorer. Because the macro was saved in Normal.dotm, it shows up in the NewMacros module. Ultimately, there could be many macros stored in that module. If the macro is to become a permanent addition to your tools, you probably want to create a module that contains common tools you create (for example, Toggles), and copy the ToggleBookmarks macro into it, rather than leave it in NewMacros to get lost as your library of macros grows. We'll look at how to create a new module and move a macro into it later in this chapter.

The right side of the window shows the contents of the NewMacros module. At the moment, it contains just one macro. If it contained more, you could use the drop-down arrow next to ToggleBookmarks to display a list of macros in the current module. Selecting a macro in the drop-down list scrolls the window to that macro.

Make sure you click Edit to edit a macro you just recorded. If you click Create rather than Edit, you will be prompted to replace the existing macro.

Figure 48.6. Make sure you click Edit to edit a macro you just recorded. If you click Create rather than Edit, you will be prompted to replace the existing macro.

When you edit or create a macro, Word opens the Microsoft Visual Basic editor.

Figure 48.7. When you edit or create a macro, Word opens the Microsoft Visual Basic editor.

In order to turn this into a toggle, there are a couple of things you're going to have to take on faith for right now. Consider the following statement:

ActiveWindow.View.ShowBookmarks = True

This statement instructs Word to set ActiveWindow.View.ShowBookmarks to True. ActiveWindow.View.ShowBookmarks is the state of the ShowBookmarks setting. If we set it to True, bookmarks will be displayed. If we set it to false, they won't be displayed. What we want to do is set it to the opposite of its current state.

As it turns out, this is ridiculously easy, and it might even seem that way if you enjoy and understand mathematics. The opposite of

ActiveWindow.View.ShowBookmarks

is

Not (ActiveWindow.View.ShowBookmarks)

Therefore, we can turn this macro into a toggle by replacing true with the latter statement:

ActiveWindow.View.ShowBookmarks= Not (ActiveWindow.View.ShowBookmarks)

Make that change in the Visual Basic editing window.

Tip

It's notoriously easy to mistype Visual Basic commands. Whenever possible, take advantage of existing code and make generous use of the Clipboard to copy and paste as needed. Here, copy ActiveWindow.View.ShowBookmarks to the Clipboard, and paste it over True on the right side of the =. Put parentheses around it, and type Not in front of it, as shown in the preceding example.

Testing Your Macro

You can test your macro without closing the Visual Basic editor. If you assigned the macro to a QAT tool or to a keyboard shortcut, switch over to the Word editing window and display something you know to be bookmarked. If the current document doesn't contain any bookmarks, then type some text, select it, press Ctrl+Shift+F5, type a bookmark name, and click Add.

Now run your toggle by clicking the corresponding QAT tool or pressing the assigned shortcut key. If it works, you should see the bookmark brackets toggling on and off.

If you didn't assign a keystroke or QAT tool, you can still test the macro, but it's a bit clumsier. Position both the Visual Basic editing window and the Word window so you can see both. In the Visual Basic editor toolbar, click the Run button shown in Figure 48.8.

When you run a macro using the Run Sub/UserForm button, it is carried out in the corresponding open Word document window.

Figure 48.8. When you run a macro using the Run Sub/UserForm button, it is carried out in the corresponding open Word document window.

Managing Macros

In order to maximize the utility of macros, there are some things you can do. As mentioned earlier, one thing you can do is create a special module for containing your macros—something other than NewMacros, which can quickly become cluttered. Another thing you can do is sign your macros so you can choose higher levels of security and still be able to run your own macros.

Copying Macros to a New Module

You can create new modules in Normal.dotm or you can create a new template for storing your macros and put that macro into your startup folder (see "Global Add-ins," later in this chapter). Using Normal.dotm is easier because it requires less work.

To create a new module in Normal.dotm, in the Project Explorer, right-click and choose Insert

Copying Macros to a New Module
When you create a new module, it is named Module# (e.g., Module1, Module2, etc.).

Figure 48.9. When you create a new module, it is named Module# (e.g., Module1, Module2, etc.).

Double-click in the NewMacros Module to display the contents of that module. Select from Sub ToggleBookmarks() through End Sub, right-click, and choose Cut (Ctrl+X). Double-click on the new module to switch back to it. (If multiple macros are listed, go to the end of an existing End Sub, and press Enter.) Right-click and choose Paste (Ctrl+V) to insert the macro at that location. If you have additional macros to transfer, repeat the procedure.

To go ahead and save your work now, choose File

When you create a new module, it is named Module# (e.g., Module1, Module2, etc.).

Note

If you've been using NewMacros as your macro repository for a while, it might sometimes seem simpler to rename NewMacros rather than create a new module and move macros over after they've been created, debugged, and tested. However, if macros are assigned to the QAT or keyboard shortcuts, those assignments will no longer work. Ultimately, it's the same amount of work, but it will seem like less work if you move macros to their permanent home on an as-you-go basis.

Digitally Signing Your Macros

This section assumes that you don't already have a certificate for signing your own macros. If you had one, you probably wouldn't be reading this section.

Installing and Running SelfCert

First, let's make sure that you can create a certificate for yourself. Choose Start

Installing and Running SelfCert

Choose Start

Installing and Running SelfCert
You can create a certificate for signing your own macro projects.

Figure 48.10. You can create a certificate for signing your own macro projects.

Note

SelfCert certificates are for personal use only on the computer on which they were created.

Signing Your Macros

If it's not already open, open the Visual Basic editor (Alt+F11). Expand the template or document that contains the macro project(s) you want to sign, click in the project you want to sign, and choose Tools

Signing Your Macros
Click Choose to pick a signing certificate.

Figure 48.11. Click Choose to pick a signing certificate.

Note

After signing a project, you might still need to close it and Word before your certificate takes effect.

Macro Security

Macro security is one of those areas that has the power to thoroughly confuse and frustrate Word users. One of the primary reasons is that a number of settings are Office-wide, and take effect when any macro-capable Office 2007 program is started. When you change settings, the changes often do not take effect until you close all macro-capable Office 2007 programs and restart. Some other settings don't take effect until just Word is closed and restarted. Let's start with four commandments. These represent elevated stages, so try them in order:

  1. When you make a change to Word's macro security settings and do not see a change in Word's macro behavior, close all instances of Word that are running and try again.

  2. When closing and restarting Word doesn't change Word's macro behavior, close all Office 2007 programs and try again.

  3. When changing a security setting and closing/restarting Office 2007 doesn't change Word's macro behavior, use Windows Task Manager to make sure all Office applications are really closed, and if not, close them and try again.

  4. When none of the above change Word's macro behavior, check whether the recalcitrance is caused by a different security setting.

Confirming Office Is Really Closed with Windows Task Manager

For many users, Windows Task Manager is an essential tool. They could not get through a day of computing without it. Many others need Windows Task Manager but simply don't know about it. There is a special name for computer users who don't need Windows Task Manager. They are called non-Windows users. Two popular varieties are Macintosh and Linux users, but we digress.

To start Windows Task Manager, press Ctrl+Alt+Delete. If pressing that key combination frightens you, right-click a blank area in Windows' taskbar either in the Quick Launch bar or in the running tasks area, and choose Task Manager instead. If you don't see it listed, try right-clicking elsewhere in the taskbar. One of these methods should start the Windows Task Manager, shown in Figure 48.12.

Use the Processes tab in Windows Task Manager to determine whether you have running instances of Office applications.

Figure 48.12. Use the Processes tab in Windows Task Manager to determine whether you have running instances of Office applications.

Look first in the Applications tab. Even if no Office applications show up there, it's possible that one or more are still running but unable to close due to an unseen problem. Click the Processes tab in Windows Task Manager and see whether an Office program is running. In particular, look for the following:

  • WINWORD.EXE (Word)

  • OUTLOOK.EXE (Outlook)

  • EXCEL.EXE (Excel)

  • MSACCESS.EXE (Access)

  • POWERPNT.EXE (PowerPoint)

  • MSPUB.EXE (Publisher)

  • SPDESIGN.EXE (SharePoint Designer)

If any of these are still running, you can try closing them using Windows Task Manager. With some, such as Outlook, however, forcing it to close can result in data loss and possible damage to your Outlook data file. Try starting Outlook normally, wait until it is fully open, and then choose File

Use the Processes tab in Windows Task Manager to determine whether you have running instances of Office applications.

Macros and Security

Keeping the commandments in mind, Word's macro behavior is governed by a number of different settings, most of which are covered in Chapter 47. When Word appears to be unresponsive to changes in security settings, it's likely due either to a need to close all of Office for the change to take effect or to layers of security that are affecting things from multiple directions. Security settings that affect whether or not macros run are as follows:

  • Macro settings—Check Office

    Macros and Security

    Warning

    When given a choice between putting a document into a trusted location versus designating the document's location as trusted, unless the location contains just that one file, it's almost always safer to move the document into a trusted location. If you set a general storage area as "trusted," it's too easy for untrustworthy files to be inadvertently saved to that location. Moving files one at a time, however, requires more thought and attention, and accidents are less likely.

  • Trusted Locations—Check Office

    Macros and Security
  • Trusted Publishers—Check Office

    Macros and Security
  • Add-ins—Check Office

    Macros and Security
  • Message Bar—Check Office

    Macros and Security
  • Unsigned Macro Project—In the Visual Basic editor, choose Tools

    Macros and Security

Macro Storage

Macros can be stored in macro-enabled document templates or in macro-enabled documents. If you upgraded to Word 2007 from an earlier version of Word, note that this is new. In the past, all Word documents had the .doc extension, and all Word templates had the .dot extension (except for a certain subset called wizards that had the .wiz extension, once upon a time).

In Word 2007, as discussed in earlier chapters, Word 2007 has four different native file formats:

  • .docx—Ordinary Word file that cannot contain macros

  • .docm—Macro-enabled document file

  • .dotx—Plain document template that cannot contain macros

  • .dotm—Macro-enabled document template file

When you record or otherwise create a macro and instruct Word to store it in a .docx or .dotx file, Word doesn't balk at that point. When you save the file, however, Word now tells you that you cannot save the VBA project in a macro-free document, as shown in Figure 48.13.

VBA projects can be saved only in .docm or .dotm files.

Figure 48.13. VBA projects can be saved only in .docm or .dotm files.

To save the macros, choose No. Word then opens the Save As dialog box. Under Save as Type, choose either Word Macro-Enabled Document (*.docm) or Word Macro-Enabled Template (*.dotm), as shown in Figure 48.14.

Global Add-ins

If you choose to store your macros in Normal.dotm, they are automatically loaded when you start Word. Any shortcut keys or Quick Access Toolbar tools assigned to run macros will run just fine, assuming that the macros and shortcuts are stored in Normal.dotm.

When a template or other add-in is available throughout the Word session, it is known as a global template or add-in. Normal.dotm by default is a global add-in. Other global add-ins can be loaded in one of two ways. They can be stored in Word's Startup folder, in which case they are loaded when Word starts, or they can be explicitly loaded using the Template and Add-ins dialog box.

Macros can be saved only in macro-enabled documents and templates.

Figure 48.14. Macros can be saved only in macro-enabled documents and templates.

To copy a file to the Startup folder, you first need to determine where that is. Choose Office

Macros can be saved only in macro-enabled documents and templates.

The other way to load a global add-in is using the Templates and Add-ins dialog box, shown in Figure 48.15. To see a list of global templates and add-ins, click Document Template in the Developer ribbon. Under Global templates and add-ins, any templates loaded from the Startup folder are listed, along with any that you have added here previously. When you open Word, only templates in the Startup folder are loaded. Other templates you've loaded in past sessions are listed, but are not checked. Place a check next to any unloaded templates you want to load. To load a template not listed, click Add. In the Add Template dialog box, the starting location is Trusted Templates. If the template you want isn't there, navigate to its location, select it, and click OK. Note, however, that depending on your macro security settings, if the template is not in a trusted location and isn't otherwise permitted, its macros will be disabled when you try to access them.

One option is to designate a .dotm file other than Normal.dotm as your main storage location for macros, and store that file in Word's StartUp folder. That way, your macros are loaded when Word starts. Moreover, if Normal.dotm becomes corrupted (which, unfortunately, is altogether likely at some point in your Word career), your task of rebuilding won't be quite as onerous, as you won't have all of your eggs in one basket, so to speak.

Note

If you choose to store your everyday macros in a .dotm file other than Normal.dotm, while the macros themselves are not stored in Normal.dotm, any key assignments or Quick Access Toolbar assignments that access those macros must be stored in Normal.dotm in order to work.

You can add and remove global templates using the Templates and Add-ins dialog box.

Figure 48.15. You can add and remove global templates using the Templates and Add-ins dialog box.

Automatic Macros

In designing document templates, as well as in setting up the Word editing environment, it sometimes is useful to perform setup or housekeeping chores. To facilitate this, Word has a collection of automatic macros you can create:

  • AutoExec—Runs when you start Word. AutoExec works only when it is stored in Normal.dotm.

  • AutoOpen—Runs when you open a document based on the template that contains it. If an AutoOpen macro is stored in Normal.dotm, then it runs anytime a document based on Normal.dotm is opened, but not when a new document based on Normal.dotm is created. A popular use for AutoOpen is to set the desired zoom or other view-related settings when documents are opened. Another popular purpose is to update certain fields.

  • AutoClose—Runs when a document based on the template that contains it is closed

  • AutoExit—Runs when you exit from Word. AutoExit works only when it is stored in Normal.dotm.

  • AutoNew—Runs when a document based on the template that contains it is created. This is a very popular kind of automatic macro to use when creating active document templates. An AutoNew document can populate a new document with content, prompt for a filename or other information, insert the current date as static text, and so on.

Macros by these names do not exist until somebody creates them. For example, if you have a set of startup chores that you perform every time you start Word, the next time you start Word, before you do your startup chores, start the macro recorder, and name the macro AutoExec. Then perform your startup chores. Stop the macro recorder when you're done. The next time you start Word, your AutoExec macro will perform your startup chores for you.

Microsoft Visual Basic Q&D

You've already learned a bit about Visual Basic and the Visual Basic editor. In this section, you'll learn only what you need to know to get started—quick and dirty, as they say. The best way to gain an understanding of Word macros is by recording a macro and then looking at the resulting Visual Basic code to see how Word translated your actions into code.

All You Really Need to Know

Visual Basic can be intimidating, as can the Visual Basic editor. Understanding what's happening can help disarm its intimidating quality. On the other hand, too much too soon can be overwhelming, and you might never want to come back. Following are just the top ten things you need to know.

1. Starting and Toggling the Visual Basic Editor

You can start the Visual Basic editor anytime you want by editing a macro or simply by starting the editor, without any particular macro in mind. To edit an existing macro or to create one, just press Alt+F8. If you see macros listed, click one, and then click Edit. If you don't see any, type a name and click Create.

To open the Visual Basic editor with no particular destination in mind, press Alt+F11. Note that once you're in the Visual Basic editor, Alt+F11 now returns the favor, and goes back to the Word document editing window. Hence, you can use Alt+F11, effectively, to toggle between the Visual Basic editor and Word.

2. Project Explorer and Code Windows

The two main sections of the Visual Basic editor you need to know about are the Project Explorer and the Code window, shown in Figure 48.16. The left side, the Project Explorer, works like any tree structure in Windows. Click+ to expand, click- to collapse. Double-click to select a macro, which is then displayed in the Code window. If the Code window is ever replaced by something else, try pressing F7 to bring the code back into view.

3. Running Macros Directly from the Visual Basic Editor

You can run macros directly from the Visual Basic editor. While it's true that this works best if you have two monitors—one for showing the Visual Basic editor and another for showing the Word document window, it's not essential. In the Windows taskbar, click the Visual Basic icon, and then Ctrl+Click the icon for the Word window. Right-click either selected program and choose Tile Horizontally. Now you should be able to see both windows at the same time.

The Visual Basic editor can look overwhelming, but there's a lot you can ignore for now.

Figure 48.16. The Visual Basic editor can look overwhelming, but there's a lot you can ignore for now.

With the macro you're testing displayed in the Code window, click the Run button, also shown in Figure 48.16. It's available in either the Standard or the Debug toolbar. If you run into an error, click Reset, fix the error, and try again. To run the macro one line at a time, click Step Into.

Additional Run-related commands are contained in the Run menu. You can also run a macro by pressing F5, but the Visual Basic editor window must be active for it to work. If the Word window is active, the F5 activates the Go To tab in the Find and Replace dialog box.

4. The Visual Basic Editor Reads Your Mind

When you type a Visual Basic command, the editor knows what "could" come next, and shows you a list of the possibilities, as shown in Figure 48.17. When you see what you want, press the spacebar to insert it. Sometimes Word shows you a list; other times, Word shows you the syntax.

When you follow a Visual Basic keyword with a period, the editor shows you a list of what can come next.

Figure 48.17. When you follow a Visual Basic keyword with a period, the editor shows you a list of what can come next.

5. The Visual Basic Editor Wants to Help

If you want more information about a command, click it and press F1. More often than not, it will show you one or more examples. How's that for helpful?

6. If the Visual Basic Window Doesn't Respond . . .

If you try clicking somewhere in the Visual Basic editing window but nothing happens, it's usually because a dialog box is open in Word itself but isn't displaying on top of the Visual Basic window. The two application windows might look independent, but they're not. Close Word's dialog box and try again.

7. If the Visual Basic Project Is "Locked," Blame Security

If you try to expand the tree in the Project Explorer but it says that the project is locked, as shown in Figure 48.18, you usually can blame those armed guards. The quickest solution is to close Visual Basic and Word, move the template that contains the macros to a Trusted Location, and then try again.

Projects can be locked for security reasons or because the project was explicitly locked by the user.

Figure 48.18. Projects can be locked for security reasons or because the project was explicitly locked by the user.

If that doesn't work, however, it's possible that the project really is locked. In the Project Explorer window, right-click the top level of the project (identified as Normal, Project, TemplateProject, or a name corresponding to a global add-in), and choose name Properties (for example, Normal Properties). In the Protection tab, remove the check next to "Lock project for viewing," supply the password, if needed, and click OK.

8. The Larger Context

When you see commands listed in the code window, you're usually not looking at the entire command. Because you're within Word, Visual Basic saves space by not including the entire name. For example, consider the following brief macro, which types Hello at the insertion point:

Sub Hello()
 Selection.TypeText Text:="Hello"
End Sub

Selection.TypeText is really an abbreviation for

Word.Application.Selection.TypeText

Because both Word and Application are assumed, however, you don't need either of those for the command to work.

9. Watch Macros as They're Being Recorded

When you start recording a macro, immediately click the Pause Recording button in the Developer tab. Press Alt+F11 to display the Visual Basic editor, and navigate to the macro being recorded. Arrange both the Word window and the Visual Basic editor window so you see both. Press Alt+F11 to return to the Word window, and click Resume Recording.

Now, as you record your macro, you'll see the corresponding commands appear in the Visual Basic editor. This is a great way to get a sense of what does what, and what kind of Visual Basic commands correspond to actions in Word. You'll also discover that some things cannot be recorded. If you watch your macro as it's being recorded, you'll know what is left out and have a better understanding of what kind of editing the macro requires in order to work properly.

10. Now You Know Why It's a Good Idea to Prompt to Save Normal.Dotm

When you're experimenting with macros, there are a lot of things you'll do that you won't necessarily want saved as part of Normal.dotm. Choose Office

10. Now You Know Why It's a Good Idea to Prompt to Save Normal.Dotm

For More Information . . .

The Word 2007 Bible is a lot of things, but it is not a book about how to program using Visual Basic for Applications. For that, you need other resources. A good place to start is the Visual Basic Help file. For starters, open the Visual Basic editor and press F1 to display Word Developer Help and How-to. Important links when you're just starting out are Getting Started, Concepts, and How Do I. Once you're more involved in Visual Basic, you'll find the Word Object Model Reference to be invaluable.

Books

For additional help in getting up to speed with Visual Basic, consult the following books from Wiley:

  • Visual Basic 6 for Dummies by Wally Wang

  • Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome

Free Online Resources

Free online resources for learning and using Visual Basic can be found at the following:

  • www.mousetrax.com/techpage.html#autoforms

  • http://word.mvps.org

Summary

In this chapter, you've gotten your feet wet with Word's brand of VBA (Visual Basic for Applications). You've seen how to record a macro and test it. You've also learned a little about how to edit macros to make them do things that the recorder can't make them do. You're now also basically familiar with the Visual Basic editor, and shouldn't feel intimidated by it. At this point, you should now be able to do the following:

  • Record a macro, and assign it to the Quick Access Toolbar or a shortcut key

  • Manage Visual Basic projects by creating new modules and copying new macros to them

  • Manage your macros by storing them in global templates other than Normal.dotm

  • Digitally sign your Visual Basic project

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.63.5