Word Secrets

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

1 2 3 Page 2
Page 2 of 3

Design the Dialog Box

The next step is to create the macro's dialog box.

First, you design the box itself.

Dialog Fun 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:

Then

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.

Toolbar Tricks 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.

1 2 3 Page 2
Page 2 of 3
  
Shop Tech Products at Amazon