By Jim Boyce
Need a project to get you through the long winter months? Dialog boxes are a hot way to organize macros, and can help simplify data entry and option selection in Word. Here I'll show you how to create and embed a dialog box into a macro. In my column next month, we'll design a macro that extracts information from a Word document and places it in the dialog box.
We'll create a dialog box for a new business report that will include controls for specifying a report title, your name and the report type (financial, marketing or technical). It will also include a check box that works with the macro we'll design next month.
To create a dialog box, use Dialog Editor. From your WINWORD directory, execute the file MACRODE.EXE. Or from the Macro toolbar, click on the Dialog Editor button. If the program isn't there, run the Office or Winword Setup program and install the Dialog Editor.
When the Dialog Editor opens, it displays a new dialog box. Let's change the dialog box's title by double-clicking on its title bar. A property sheet for the dialog box is now displayed. In the Text$ text box, type the title Begin a Report for this example. Then click on OK.
Next, drag a corner of the dialog box to make it a bit larger. Now choose Item/Text to insert a text object into the dialog box. Double-click on the text object to open its property sheet, and change the Text$ field to Report Title. Then choose Item/Text Box to insert a text box; it will appear below the Report Title text object.
Repeat these steps to insert a second text object labeled Author, with a corresponding text box below it, and a third text object labeled Document Title, with a corresponding text box. Resize them to suit your preferences. If you're confused about where the objects should be located, see the sidebar "Delightful Dialog" (which includes a picture of your completed dialog box).
Now, choose Item/Group Box to insert a group box to hold the option buttons. Double-click on the inserted group box and change its Text$ field to read Report Type. Drag one of the group box's corners to make it large enough to contain the three option buttons. With the group box still selected, choose Item/Button/Option to insert an option button. Repeat this process to insert two more option buttons. Double-click on the topmost option button and change its Text$ field to read &Financial. Do not insert a space between the ampersand (&) and the word Financial. The ampersand causes the next character (F in this case) to be underlined and act as a shortcut key for the control, enabling you to select the control with the keyboard when the dialog box is active. Change the Text$ field of the second option button to &Marketing, and the third option button to &Technical.
Move the group box and option buttons to the upper right-hand portion of the dialog box. To do so, click on the group box, then hold down the Shift key and click the three option buttons. Drag the selected objects into the desired position.
Now let's add a line that gives you the option to include a Web address. Choose Item/Button/Check Box to insert a check box, then double-click on the check box and set Text$ to read Include &Web Address. Insert one last text box and position it below the check box object. Add an OK button by choosing Item/Button/OK. Finally, insert a Cancel button by choosing Item/Button/Cancel. Drag them into position (as shown in the sidebar).
When you're satisfied with the dialog box's layout, start creating your macro (we'll finish it next month) and add the dialog box to it. Open Word and begin a new document. Choose Tools/Macro to open the Macro dialog box. In the Macro Name text box, type the macro's name. For this example, type StartReport. Then click on Create to begin a new macro document. Word starts the document and automatically includes the Sub MAIN and End Sub statements, which bracket the main procedure of the macro.
Switch to the Dialog Editor and choose Edit/Select Dialog. Next, choose Edit/Copy. Switch back to Word and make sure the insertion point is located between the Sub MAIN and End Sub statements, then choose Edit/Paste to paste the dialog box into the macro. The Macro will display the proper WordBasic statements that define the dialog box.
We won't add any code to fill the dialog box or use its contents this month, but you'll want to test it in Word. Scroll to the end of the macro. After the End Dialog statement but before the End Sub statement, enter the following code:
Dim Dlg As UserDialog
ReturnValue = Dialog(Dlg)
The Dim statement defines a dialog record to contain the dialog box contents. Note that the Dialog Editor automatically used the name UserDialog for the dialog box definition. By passing the name of the dialog box (UserDialog) to the Dim statement, you define a dialog box record for that specific dialog box. If you had defined other dialog
boxes with other names within the macro, you would specify their names with the Dim statement to define records for them. However, you can only define one custom dialog box record at a time. Defining a new custom dialog box record clears the previous one, so any settings you want to retain from a dialog box should be stored (through code you write) to standard variables.
The Dialog() function displays the dialog box, and I've passed it the name of the dialog box record of the dialog box I want to display. In this example, when you close the dialog box, WordBasic returns a value that is stored in the variable ReturnValue (use your own variable name, if you want). If the value of ReturnValue is -1, the OK button was pushed. A value of zero indicates the Cancel button was pushed.
I'll leave you with just a bit more code to test the dialog box and whet your appetite for next month. Briefly, the following code creates a dialog box record for the Document Statistics dialog box and updates its contents. Then, the contents of some of the Document Statistics fields are placed in three of the text boxes in your custom dialog box:
Dim Dlg2 As Document
Dlg1.TextBox1 = Dlg2.Title
Dlg1.TextBox2 = Dlg2.Last
Dlg1.TextBox3 = Dlg2.FileName
Place the above code before the statement:
ReturnValue = Dialog(Dlg)
Save the macro, then run it to test your dialog box. (Tip: Save the document before running the macro.)
Contributing Editor Jim Boyce is the lead author of Windows NT Advanced Technical Reference (Que, 1996). Contact Jim in the "Applications" topic of WINDOWS Magazine's areas on America Online and CompuServe, or e-mail him at email@example.com