Share via


How to: Use Built-In Dialog Boxes in Word

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

When working with Microsoft Office Word, there are times when you need to display dialog boxes for user input. Although you can create your own, you might also want to take the approach of using the built-in dialog boxes in Word, which are exposed in the Dialogs collection of the Application object. This enables you to access over 200 of the built-in dialog boxes, which are represented as enumerations.

To use a built-in dialog box

  1. Use one of the values of the WdWordDialog enumeration to create a Dialog object that represents the Word dialog box you want to display. To use the following code example, run it from the ThisDocument or ThisAddIn class in your project.

    Dim dlg As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFileNew)
    
    Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFileNew];
    
  2. After you create the Dialog variable, you can call its methods.

    dlg.Show()
    
    object timeOut = 0;
    dlg.Show(ref timeOut);
    

To access dialog box members

  1. Get the dialog box type and set the Name property to Testing. To use the following code example, run it from the ThisDocument or ThisAddIn class in your project.

    Note

    Interactions with built-in Word dialog boxes occur through late binding, so if you have Option Strict set to On or you use C#, you cannot access members of the dialog boxes directly. You must use the Reflection libraries to access dialog box members.

    Dim dlg As Word.Dialog = Application.Dialogs(Word.WdWordDialog.wdDialogFileOpen)
    Dim dlgType As Type = GetType(Word.Dialog)
    
    ' Set the Name property of the dialog box.
    dlgType.InvokeMember("Name", _
        Reflection.BindingFlags.SetProperty Or _
            Reflection.BindingFlags.Public Or _
            Reflection.BindingFlags.Instance, _
        Nothing, dlg, New Object() {"Testing"}, _
        System.Globalization.CultureInfo.InvariantCulture)
    
    Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
    System.Type dlgType = typeof(Word.Dialog);
    
    // Set the Name property of the dialog box.
    dlgType.InvokeMember("Name", 
        System.Reflection.BindingFlags.SetProperty | 
            System.Reflection.BindingFlags.Public | 
            System.Reflection.BindingFlags.Instance,
        null, dlg, new object[] {"Testing"},
        System.Globalization.CultureInfo.InvariantCulture);
    
  2. Show the dialog box, and then show the Name property in a message box.

    ' Display the dialog box.
    dlg.Show()
    
    ' Show the Name property.
    MessageBox.Show(dlgType.InvokeMember("Name", _
        Reflection.BindingFlags.GetProperty Or _
            Reflection.BindingFlags.Public Or _
            Reflection.BindingFlags.Instance, _
        Nothing, dlg, Nothing, _
        System.Globalization.CultureInfo.InvariantCulture))
    
    // Display the dialog box.
    dlg.Show(ref missing); 
    
    // Show the Name property.
    MessageBox.Show(dlgType.InvokeMember("Name",
        System.Reflection.BindingFlags.GetProperty |
            System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance,
        null, dlg, null,
        System.Globalization.CultureInfo.InvariantCulture).ToString());
    

See Also

Tasks

How to: Use Word Dialog Boxes in Hidden Mode

Concepts

Word Object Model Overview

The Variable missing and Optional Parameters in Office Solutions