How to Use VBA to Show Custom Dialog Box in Word
🔍 WiseChecker

How to Use VBA to Show Custom Dialog Box in Word

You want to create a custom dialog box in Word using VBA to collect user input or display options. The built-in Word dialogs cannot be fully customized for your specific workflow. This article explains how to build a UserForm dialog box with VBA, add controls, and retrieve the values the user enters. You will learn the exact steps to design, code, and launch your own dialog box inside Word.

Key Takeaways: Building a Custom VBA Dialog Box in Word

  • Insert > UserForm in the VBA Editor: Creates a blank canvas for your custom dialog box.
  • Toolbox controls (TextBox, ComboBox, CommandButton): Add input fields and buttons to your UserForm.
  • UserForm.Show method: Launches the dialog box from a macro so users can interact with it.

What Is a VBA UserForm and Why Use It

A UserForm is a dialog box you design in the Visual Basic Editor. Unlike Word’s built-in dialogs, a UserForm can contain any combination of text boxes, drop-down lists, check boxes, option buttons, and other controls. You write VBA code behind the form to handle what happens when the user clicks a button or changes a value.

You need the Developer tab enabled in Word to access the VBA Editor. If you do not see the Developer tab, go to File > Options > Customize Ribbon and check the Developer box in the right panel. The VBA Editor is opened by pressing Alt+F11.

A custom dialog box is useful when you need to collect multiple pieces of information before running a macro. For example, you might ask the user to enter a document title, select a template name from a list, and choose a formatting option. The UserForm stores these values in variables that your macro can use.

Steps to Create a Custom Dialog Box With VBA

  1. Open the VBA Editor and insert a UserForm
    Press Alt+F11 to open the VBA Editor. In the Project Explorer pane, right-click Normal or your current document project. Choose Insert > UserForm. A blank form named UserForm1 appears in the editor window.
  2. Set the UserForm properties
    With the UserForm selected, press F4 to open the Properties window. Change the Caption property to a descriptive title such as “Document Setup”. Change the Name property to something meaningful like frmDocumentSetup. Set Width and Height to values that fit your controls, for example 400 by 300.
  3. Add controls from the Toolbox
    If the Toolbox is not visible, go to View > Toolbox. Drag a Label control onto the form and set its Caption to “Document Title:”. Drag a TextBox next to the label. Drag a ComboBox below the label and set its RowSource property later in code. Drag two CommandButton controls and set their Caption properties to “OK” and “Cancel”.
  4. Write code for the OK and Cancel buttons
    Double-click the OK button. The VBA editor opens the code window for that button. Enter the following code:
    Private Sub cmdOK_Click()
    Me.Hide
    End Sub

    Double-click the Cancel button and enter:
    Private Sub cmdCancel_Click()
    Me.Hide
    End Sub

    Later you will add code to read the values before hiding.
  5. Write code to populate the ComboBox when the form loads
    In the code window, select UserForm from the left drop-down and Initialize from the right drop-down. Enter:
    Private Sub UserForm_Initialize()
    Me.cboTemplates.AddItem "Report"
    Me.cboTemplates.AddItem "Letter"
    Me.cboTemplates.AddItem "Memo"
    End Sub

    Replace cboTemplates with the actual name of your ComboBox.
  6. Write a macro to show the UserForm and retrieve values
    Insert a new module (Insert > Module) and enter:
    Sub ShowMyDialog()
    Dim strTitle As String
    Dim strTemplate As String
    frmDocumentSetup.Show
    strTitle = frmDocumentSetup.txtDocumentTitle.Text
    strTemplate = frmDocumentSetup.cboTemplates.Text
    MsgBox "Title: " & strTitle & vbCrLf & "Template: " & strTemplate
    End Sub

    Run the macro by pressing F5 while in the module or by assigning it to a button on the Quick Access Toolbar.
  7. Test the dialog box
    Close the VBA Editor and return to Word. Press Alt+F8 to open the Macros dialog. Select ShowMyDialog and click Run. Your custom dialog box appears. Enter a title, select a template, and click OK. A message box displays the values you entered.

Common Mistakes When Creating Custom Dialog Boxes

The UserForm Does Not Appear When I Run the Macro

The most common cause is that the UserForm name in the Show method does not match the actual Name property of the form. Check the Name property in the Properties window. If you renamed it to frmDocumentSetup, make sure your macro uses that exact name. Also confirm that the macro is in the same project as the UserForm.

The ComboBox Is Empty When the Form Opens

The ComboBox items must be added in the UserForm_Initialize event. If you added the items in a different event or in the module code, they will not appear. Open the UserForm code window and verify that the Initialize event contains the AddItem lines. Make sure the ComboBox name in the code matches the actual control name.

The Form Closes But the Values Are Lost

When you use Me.Hide, the form is hidden but not unloaded. The control values remain accessible as long as the form is not unloaded. If you use Unload Me instead of Me.Hide, the form and its values are destroyed. Use Me.Hide in the OK button and only call Unload Me in the Cancel button if you do not need the values. Alternatively, declare public variables in the module and assign the control values to them before hiding the form.

Controls Overlap or Are Misaligned

In the VBA Editor, use the Format menu to align controls. Select multiple controls by holding Ctrl while clicking each one. Then choose Format > Align > Lefts to align them vertically. Use Format > Horizontal Spacing > Make Equal to distribute controls evenly. Set the TabIndex property of each control to define the order users move through fields when pressing Tab.

VBA UserForm vs InputBox: Which Should You Use

Item UserForm InputBox
Number of inputs Unlimited One per call
Control types Text box, combo box, check box, option button, list box Text entry only
Validation Custom VBA code in button click events None built-in
Visual design Fully customizable size, color, font, layout Fixed appearance, no customization
Code complexity Moderate — requires form design and event handlers Minimal — single function call
Best use case Multiple related inputs or complex data entry Quick single-value prompts

You can now create a custom dialog box in Word using VBA UserForms. Start by designing a form with the necessary controls, write the Initialize event to populate lists, and add button handlers to retrieve the user’s input. For advanced use, add validation that checks if required fields are empty before hiding the form. Use the Tag property of controls to store metadata that your macro reads later.