Word Secrets

There's no need to trudge through the same steps in Microsoft Word again and again. Macros can automate a wide range of tasks, from inserting and formatting text to changing program preferences. You may have dabbled with simple macros, but creating smarter ones controlled by dialog boxes can make your Mac do even more of your work for you.

For example, a macro-generated dialog box can offer two choices, and the macro can then change multiple Word settings when an option is chosen. Here you'll write a macro that lets you choose either Web or print formatting for a document. When you choose one of the options, the macro will alter Word's quotation-mark and hyperlink settings appropriately.

You'll learn to create a dialog box, add buttons to it, and set up those buttons to do as you ask. Once you've completed this project, you can use the same technique to create other dialog boxes that change Word settings.

First Steps

If you've written Word documents for print and for the Web, you know they require different formatting. In print, curly quotation marks ("smart quotes") look more professional than straight ones, and e-mail addresses and URLs don't need to be active hyperlinks. (It's not much use trying to click on the printed page!) In Web pages, hyperlinks come in handy, but straight quotes are the standard. This is the perfect opportunity to create a macro that simplifies your everyday work.

Not Your Average Macro Since this macro includes a dialog box, you can't simply record steps with Word's Record New Macro tool. You must also design the dialog box, buttons and all. Then you'll write the code that makes each button do something, as well as a small piece of code that displays the dialog box when you need it.

To do all this, you'll use Word's Visual Basic Editor, the same programming language used in other Office applications -- such as Excel and PowerPoint -- to automate tasks. When you record a macro, Word writes the Visual Basic code for you behind the scenes.

Code Hunt Before you make your dialog box, you need to find the code that turns smart quotes and hyperlinks on and off. (Save this code to use when you set up your dialog-box buttons.) You could look in Visual Basic's Help (in Microsoft Word, choose Tools: Macro: Visual Basic Editor, and then choose Help). However, the simpler route is to get Word to tell you what the code is.

To do this, record a macro. Choose Tools: Macro: Record New Macro. A dialog box will appear. Enter a name -- for example, Test -- in the Macro Name field, and click on OK to start recording. Word watches your every move and records the code necessary to repeat your steps. To turn on smart quotes, choose Tools: AutoCorrect. Click on the AutoFormat As You Type tab, and select the "Straight Quotes" With "Smart Quotes" option. While you have the AutoCorrect dialog box open, turn off hyperlinks by deselecting the Internet Paths With Hyperlinks option.

When you're done, click on OK to close the AutoCorrect dialog box. Then click on the Stop Recording button (on the toolbar that appears when you're recording a macro). Choose Tools: Macro: Macros, and select your macro -- in this example, Test -- from the list. Click on Edit to see all the code that Word recorded; Visual Basic Editor will open.

You must sift through many lines of instructions to find the code that relates to the smart quotes and hyperlinks settings:

From these Visual Basic statements, it's clear that to turn the settings on you set them to True, and to turn them off you set them to False. The With statement is Visual Basic shorthand, eliminating the need to refer to the Options object in each statement. (It also speeds up the commands when they run.) You could also write the lines of code like this, and they'd work the same way:

Now you know how to tell Word to turn these options on and off. Take note of the code.

Subscribe to the Best of Macworld Newsletter

Comments