You can display a built-in dialog box to get user input or to control Word by using Visual Basic. The Show method of the Dialog object displays and executes any action taken in a built-in Word dialog box. To access a particular built-in Word dialog box, you specify a WdWordDialog constant with the Dialogs property. For example, the following macro instruction displays the Open dialog box (wdDialogFileOpen).
Dialogs(wdDialogFileOpen).Show
If a file is selected and OK is clicked, the file is opened (the action is executed). The following example displays the Print dialog box (wdDialogFilePrint).
Dialogs(wdDialogFilePrint).Show
Set the DefaultTab property to access a particular tab in a Word dialog box. The following example displays the Page Border tab in the Borders and Shading dialog box (Format menu).
With Dialogs(wdDialogFormatBordersAndShading)
.DefaultTab = wdDialogFormatBordersAndShadingTabPageBorder
.Show
End With
The Display method displays a dialog box without executing the actions taken in the dialog box. This can be useful if you want to prompt the user with a built-in dialog box and return the settings. For example, the following macro instruction displays the User Information tab from the Options dialog box and then returns and displays the user name.
With Dialogs(wdDialogToolsOptionsUserInfo)
.Display
MsgBox .Name
End With
If the user name is changed in the previous example, the change is not set in the dialog box. Use the Execute method to execute the settings in a dialog box without displaying the dialog box. The following example displays the User Information dialog box, and if the name is not an empty string, the settings are set in the dialog box using the Execute method.
With Dialogs(wdDialogToolsOptionsUserInfo)
.Display
If .Name <> "" Then .Execute
End With
Returning and changing dialog box settings
Prior to returning or changing a dialog box setting, you need to identify the individual dialog box. This is done using the Dialogs property with a WdWordDialog constant. The following example returns a Dialog object that refers to the Paragraph dialog box (Format menu).
Set myDialog = Dialogs(wdDialogFormatParagraph)
After you have a Dialog object you can return or sets options in the dialog box. The following example displays the right indent from the Paragraphs dialog box.
Set myDialog = Dialogs(wdDialogFormatParagraph)
Msgbox "Right indent = " & myDialog.RightIndent
Many of the built-in Word dialog boxes have arguments that you can use to set or get values from a dialog box (for example, RightIndent in the previous example). To determine which arguments you can use, see Built-in dialog box argument lists.
Just as you can return dialog box settings, you can also set dialog box settings. The following example marks the Keep with next check box in the Paragraph dialog box.
With Dialogs(wdDialogFormatParagraph)
.KeepWithNext = 1
.Execute
End With
The Keep with next check box is enabled and the Execute method sets the changed settings value in the dialog box. The following Visual Basic instructions is the equivalent of the four instructions in the previous example.
Selection.Paragraphs(1).KeepWithNext = True
It's not very efficient to use a Dialog object to change a value that you can set with a property or method.
Note Use the Update method to ensure that the dialog box values reflect the current values. It may be necessary to use the Update method if you define a dialog box variable early in your macro and later want to return or change the current settings.
Presetting dialog box settings
The previous examples have return and set dialog box values without displaying the dialog box. You can also can change settings in a built-in Word dialog box prior to using the Show method. For example, you can change the find text before displaying the Find and Replace dialog box (Edit menu). The following example displays the Find and Replace dialog box with the word "Blue" preset in the Find what edit box.
With Dialogs(wdDialogEditFind)
.Find = "Blue"
.Show
End With
The following example displays the Open dialog box with all file names displayed.
With Dialogs(wdDialogFileOpen)
.Name = "*.*"
.Show
End With
Checking how a dialog box was closed
The value returned by the Show and Display methods indicates which button was clicked to close the dialog box. The following example displays the Break dialog box, and if OK is clicked, a message is displayed on the status bar.
If Dialogs(wdDialogInsertBreak).Show = -1 Then
StatusBar = "Break inserted"
End If
Return value | Description |
---|---|
-2 | The Close button. |
-1 | The OK button. |
0 (zero) | The Cancel button. |
> 0 (zero) | A command button: 1 is the first button, 2 is the second button, and so on. |