Share via


Working with Word Dialog Boxes

You can create your own custom dialog boxes by using UserForms, but before you do, you should determine whether you could simply appropriate the functionality of one of Word's more than two hundred built-in dialog boxes.

From VBA, you access any of Word's built-in dialog boxes through the Dialogs collection. The Dialogs collection is a global object, so you can reference it without specifying the Application property. For example, you can run the following code from the Immediate window to return the number of dialog boxes in the Dialogs collection:

? Dialogs.Count

To work with a particular dialog box, you create an object variable declared As Dialog and use one of the wdWordDialog constants to specify the dialog box you want to reference. For example, the following code creates a reference to the Spelling and Grammar dialog box:

Dim dlgSpell As Dialog
Set dlgSpell = Dialogs(wdDialogToolsSpellingandGrammar)

When you instantiate a Dialog object variable in this way, you can easily determine or specify the various dialog box settings. When you refer to one of these settings in VBA, you can reference it as a property of the dialog box. For example, you can refer to the All setting on the View tab of the Options dialog box by using the ShowAll property:

MsgBox "The 'All' setting on the View tab is currently set to " _
   & CBool(Dialogs(wdDialogtoolsOptionsView).ShowAll)

Dialog box properties are typically set from the user interface by using check box controls, combo box controls, or text box controls. Check box controls contain the value 1 when they are selected and 0 when they are not selected. Combo box controls contain the index value of the item selected, beginning at 0 for the first item in the control. Text box controls contain a String value representing the text in the control.

You also have control over how a dialog box is displayed and when changes to settings take effect. When you display a dialog box from the Word user interface and change a setting, the change usually takes effect as soon as you click the dialog box's OK button, although some dialog box settings take effect immediately. When you use VBA to display a dialog box, you can control how the dialog box behaves by using either the Dialog object's Show method or its Display method. If you use the Show method, the dialog box behaves just as it does when Word displays it. The Display method simply displays the dialog box and you must use additional VBA code to take further action in response to any selections made in the dialog box by the user. Both methods also return a value representing whether the user clicked OK, Cancel, Close, or some other button in the dialog box.

See Also

Working with Microsoft Word Objects | Understanding Application-Level Objects | Working with the Application Object | Working with the Settings in the Options Dialog Box | Modifying Built-in Commands