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.
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.
Design the Dialog Box
The next step is to create the macro’s dialog box.
First, you design the box itself.
Open Visual Basic Editor (Tools: Macro: Visual Basic Editor). You’ll need to use the Properties window to adjust some of the default properties for your buttons. If it isn’t visible (its name appears in its title bar), choose View: Properties Window. In the Projects window, find the Normal entry, and click on it to select it. This ensures that the macro will be saved in the Normal template, so it will be available to all new Word documents.
From the Application menu, choose Insert: UserForm. (Visual Basic calls dialog boxes forms.) A small gray grid will appear in a new window. This is the dialog box’s starting point. Now it’s just a plain blank box, ready for customization. You should also see a Toolbox toolbar; if it isn’t there, choose View: Toolbox. You’ll need this to add elements to your dialog box.
Locate the OptionButton, CommandButton, and Label controls in the Toolbox toolbar. If you’re unsure which is which, hold your mouse over each control and read its yellow screen tip. You’ll need one Label control at the top of the dialog box (this will tell you what the dialog box can do), two OptionButton controls under it (these will offer two choices — to set up either a Web or a print document), and finally two CommandButtons side by side across the bottom (these will become your OK and Cancel buttons).
Click on each control and drag to place it on the gray box. Don’t worry too much about getting the size and position exactly right the first time — it’s easy to resize and move the controls later.
The Proper Properties
Now you can customize these buttons. Each one has a number of settings (called properties), which determine its name, how it looks, and what it can do.
To begin, click on an empty area of the dialog box to select it. Note that the Properties window says UserForm1 (or UserForm2, and so forth, if you already have another dialog-box–based macro). You’ll want to change its title so that the title bar gives some indication as to its purpose. To do this, look for the Caption property in this Properties window and type Quick Document Setup in place of the words in the field next to it. Next, you’ll want to change the dialog box’s name so you can refer to it in your code later.
To do this, set the Name property to read frmSetup.
Now it’s time to set up the option buttons so they are more descriptive and have names you can refer to in your code. Click on the top option button and type optPrint in the field next to Name and Document for print in the field next to Caption. Type P in the field next to Accelerator. Set the Value property to True.
To set up the second option button, click on it to select it. Change its Name property to optWeb, its Caption property to Document for Web, and its Accelerator property to W.
The Name property is the name you’ll use in your code to refer to the option button. The Caption property is the text that appears in the dialog box, to the right of the option button. Setting the first option button’s Value property to True ensures that it is selected by default when the dialog box is displayed. The Accelerator settings let you select these options from the keyboard by pressing Command-P or Command-W. If only part of the caption text shows, the control box is too small. Widen it to show all the text.
Now you can set up the OK and Cancel buttons. Click on the left CommandButton, which will be the Cancel button, and type cmdCancel in the field next to Name and Cancel in the field next to Caption. Type C in the field next to Accelerator. Finally, select True from the pop-up menu next to Cancel to ensure that you can choose Cancel not only by pressing this button but also by pressing the escape key on your keyboard. Click on the right CommandButton, which will be the OK button, and set its Name property to cmdOK, its Caption property to OK, and its Accelerator property to O.
Now you’ll set up the label, which displays instructions for using the dialog box. Click on the Label button to select it. In the Property window next to Caption, type Choose document type and click on OK. If necessary, resize the label to show all the text.
Write the Macro Code
This dialog box can’t actually do anything — yet. Next you must write the code that tells Word what to do when someone clicks on the buttons. Now you’ll make use of the code you found earlier.
When you click on OK, the macro needs to find out which option — Document For Print or Document For Web — you’ve selected and then change the smart-quote and hyperlink settings appropriately. After that, the dialog box should disappear. If you click on Cancel, the macro shouldn’t change any settings; the dialog box should disappear.
Easy Options The option buttons don’t need any additional code — the settings you typed in Visual Basic Editor are enough. When you click on an option button, its Value property changes to True and the other option button’s Value property automatically changes to False. In fact, in the code you’ll add next, you’ll use this behavior to see which option (Web or Print) has been selected.
The Critical Code
To make the rest of the buttons trigger actions, you’ll write some code. Don’t worry — it isn’t case sensitive. To ease reading, you may indent some of the code by pressing the tab key. Double-click on the OK button you created to open Visual Basic’s Code window. You’ll see these lines on screen:
Type this text between the first and last lines:
This code checks to see whether the Value property of the button we named optPrint is set to True. If it is, the Print option is selected and the next lines of code turn smart quotes on and disable hyperlinks. You can select only one of the two options at a time, so if the optPrint button isn’t selected — setting the value to False — then the optWeb button must be. In that case, the next lines of code turn off the smart quotes and turn on the hyperlink options.
The only thing left to do is write the code for the Cancel button. Type this code immediately after the End Sub line in the previous code:
Now the dialog box will go away if you click on Cancel. However, you still need a small piece of code that makes the dialog appear when you start the macro. Choose Insert: Module and type this in the Code window that appears:
To save your work, click on the Save button and choose Word: Close And Return To Microsoft Word.
Test and Troubleshoot
Test the macro by choosing Tools: Macro: Macros. Select DocumentSetup from the list and click on Run. If you encounter problems, an error message will appear. Click on Debug, and Visual Basic Editor will open, highlighting the statement that’s probably to blame. Check your spelling!
If all goes well, your dialog box should appear. You can test the buttons and the two shortcuts, Command-O (OK) and Command-C (Cancel). See if the smart-quote and hyperlink settings work correctly by opening a new document and choosing either Web or print formatting from the dialog box that appears. Type a set of quotation marks and a Web address.
Once your macro works as planned, you can add it to Word’s toolbar so it will always be available. Choose View: Toolbars: Customize, and choose the Commands tab. In the Categories list, choose Macros. From the commands list, drag Normal.Module1,documentSetup onto the toolbar. That’s not very eloquent, so you may want to change it. Control-click on the button, choose Properties, and type something shorter. Click on OK twice.
The Last Word
Now that you’ve seen how easy it is to create your own working dialog boxes, you can use this method to change other Word preferences. With a little bit of effort, you can create macros that save you a whole lot of time.