SoFunction
Updated on 2025-04-06

C# Use built-in dialog box instance in Word

This article describes the method of using built-in dialog boxes in Word in C#, and is shared with you for your reference. The specific implementation method is as follows:

When using Microsoft Office Word, sometimes the user input dialog box needs to be displayed. While you can create your own dialogs, you may also want to use the built-in dialogs in Word that are exposed in the Dialogs collection of Application objects. This gives you access to more than 200 built-in dialogs, represented in enumerations.

Applicable to: The information in this article applies to document-level and application-level projects in Word 2013 and Word 2010. For more information, see Features provided by Office Application and Project Type: /zh-cn/library/vstudio/.

Display the dialog box:

To display a dialog box, create a Dialog object using one of the values ​​of the WdWordDialog enumeration, which represents the dialog box to be displayed. Then, call the Show method of the Dialog object.

The following code example demonstrates how to display the Open dialog box. To use this example, run this example from the ThisDocument or ThisAddIn class inside the project.

Copy the codeThe code is as follows:
dlg = [];
();

Access dialog members that can be used through later binding

Some properties and methods of dialog boxes in Word can only be used through post-binding. Open in the Visual Basic project Option Strict location, you must use reflection to access these members. For more information, see Post-binding in Office solutions: /zh-cn/library/vstudio/.

The following code example demonstrates how to use the Name property of the File Open dialog box in Option Strict or Visual Basic project in Visual C# project for .NET Framework 4 or .NET Framework 4.5. To use this example, run this example from the ThisDocument or ThisAddIn class inside the project.

Copy the codeThe code is as follows:
dynamic dialog = [];
= "Testing";
();
();

The following code example demonstrates how to use reflection to file open dialog Name property opens in Visual Basic Project Access Option Strict location. To use this example, run this example from the ThisDocument or ThisAddIn class inside the project.

Copy the codeThe code is as follows:
Dim dlg As = ()
Dim dlgType As Type = GetType()

' Set the Name property of the dialog box.
("Name", _
    Or _
        Or _
        , _
    Nothing, dlg, New Object() {"Testing"}, _
    )

' Display the dialog box.
()

' Show the Name property.
(("Name", _
    Or _
        Or _
        , _
    Nothing, dlg, Nothing, _
    ))

I hope this article will be helpful to everyone's C# programming.