The examples in this article describe how C # uses the built-in dialog boxes in Word to share them for your reference. The implementation method is as follows:
When you use Microsoft Office Word, you sometimes need to display the user Input dialog box. Although you can create your own dialog boxes, you might also want to use the methods of using built-in dialog boxes in Word that are exposed in the Dialogs collection of the Application object. This gives you access to more than 200 built-in dialog boxes, which are represented as enumerations.
Applies to: The information in this article applies to document-level and application-level projects for Word 2013 and Word 2010. For more information, see Features available by Office applications and project types: http://msdn.microsoft.com/zh-cn/library/vstudio/aa942839.aspx.
Show dialog box:
To display a dialog box, use one of the values of the WdWordDialog enumeration to create the dialog object that represents the dialog box that you want to display. Then, call the Show method of the dialog object.
The following code example shows how to display the Open dialog box. To use this example, run the sample from the ThisDocument or ThisAddIn class within the project.
Copy CodeThe code is as follows: Word.dialog dlg = Application.dialogs[word.wdworddialog.wddialogfileopen];
Dlg. Show ();
To access a dialog box member that can be used by late binding
Some properties and methods of dialog boxes in Word can only be used by late binding. When you open the Visual Basic Project Option Strict location, you must use reflection to access these members. For more information, see late Binding in Office solutions: http://msdn.microsoft.com/zh-cn/library/vstudio/3xxe951d.aspx.
The following code example demonstrates how to use the Name property of the file opened dialog box in Option Strict or a visual Basic project in a Visual C # project that targets the. NET Framework 4 or. NET Framework 4.5. To use this example, run the sample from the ThisDocument or ThisAddIn class within the project.
Copy CodeThe code is as follows: Dynamic dialog = Application.dialogs[word.wdworddialog.wddialogfileopen];
Dialog. Name = "testing";
Dialog. Show ();
MessageBox.Show (dialog. Name);
The following code example demonstrates how to use reflection to open a file that has the Open dialog box Name property accessed in Visual Basic for an item in the Option strict location. To use this example, run the sample from the ThisDocument or ThisAddIn class within the project.
Copy CodeThe code is as follows: 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)
' 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))
I hope this article is helpful to everyone's C # programming.
C # uses a built-in dialog box instance in Word