How to: Use Word Dialog Boxes in Hidden Mode
You can perform complex operations with one method call by invoking the built-in dialog boxes in Microsoft Office Word without displaying them to the user. You can do this by using the Execute method of the Dialog object without calling the Display method.
Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.
Examples
The following code examples demonstrate how to use the Page Setup dialog box in hidden mode to set multiple page setup properties with no user input. The examples use a Dialog object to configure a custom page size. The specific settings for page setup, such as the top margin, bottom margin, and so on, are available as late-bound properties of the Dialog object. These properties are dynamically created by Word at run time.
The following example demonstrates how to perform this task in Visual Basic projects where Option Strict is off and in Visual C# projects that target the .NET Framework 4. In these projects, you can use late binding features in the Visual Basic and Visual C# compilers. To use this example, run it from the ThisDocument or ThisAddIn class in your project.
Dim dialog As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFilePageSetup)
' Set the properties of the dialog box.
' ControlChars.Quote() is used to represent the symbol for inches.
With dialog
.PageWidth = 3.3 & ControlChars.Quote
.PageHeight = 6 & ControlChars.Quote
.TopMargin = 0.71 & ControlChars.Quote
.BottomMargin = 0.81 & ControlChars.Quote
.LeftMargin = 0.66 & ControlChars.Quote
.RightMargin = 0.66 & ControlChars.Quote
.HeaderDistance = 0.28 & ControlChars.Quote
.Orientation = Word.WdOrientation.wdOrientPortrait
.DifferentFirstPage = False
.FirstPage = 0
.OtherPages = 0
' Apply these settings only to the current selection with this line of code:
.ApplyPropsTo = 3
' Apply the settings.
.Execute()
End With
dynamic dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];
dialog.PageWidth = "3.3\"";
dialog.PageHeight = "6\"";
dialog.TopMargin = "0.71\"";
dialog.BottomMargin = "0.81\"";
dialog.LeftMargin = "0.66\"";
dialog.RightMargin = "0.66\"";
dialog.HeaderDistance = "0.28\"";
dialog.Orientation = "0";
dialog.DifferentFirstPage = "0";
dialog.FirstPage = "0";
dialog.OtherPages = "0";
// Apply these settings only to the current selection with this line of code:
dialog.ApplyPropsTo = "3";
// Apply the settings.
dialog.Execute();
The following example demonstrates how to perform this task in Visual Basic projects where Option Strict is on and in Visual C# projects that target the .NET Framework 3.5. In these projects, you must use reflection to access the late-bound properties. To use this example, run it from the ThisDocument or ThisAddIn class in your project.
Friend Sub PageSetupDialogHidden()
Dim dialog As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFilePageSetup)
' Set the properties of the dialog box.
' ControlChars.Quote() is used to represent the symbol for inches.
InvokeHelper(dialog, "PageWidth", "3.3" & ControlChars.Quote)
InvokeHelper(dialog, "PageHeight", "6" & ControlChars.Quote)
InvokeHelper(dialog, "TopMargin", "0.71" & ControlChars.Quote)
InvokeHelper(dialog, "BottomMargin", "0.81" & ControlChars.Quote)
InvokeHelper(dialog, "LeftMargin", "0.66" & ControlChars.Quote)
InvokeHelper(dialog, "RightMargin", "0.66" & ControlChars.Quote)
InvokeHelper(dialog, "HeaderDistance", "0.28" & ControlChars.Quote)
InvokeHelper(dialog, "Orientation", "0")
InvokeHelper(dialog, "DifferentFirstPage", "0")
InvokeHelper(dialog, "FirstPage", "0")
InvokeHelper(dialog, "OtherPages", "0")
' Apply these settings only to the current selection with this line of code:
InvokeHelper(dialog, "ApplyPropsTo", "3")
' Apply the settings.
dialog.Execute()
End Sub
Private Shared Sub InvokeHelper(ByVal dialog As Word.Dialog, ByVal member As String, ByVal value As String)
Dim dialogType As System.Type = GetType(Word.Dialog)
' Set the appropriate property of the dialog box.
dialogType.InvokeMember(member,
System.Reflection.BindingFlags.SetProperty Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance,
Nothing, dialog, New Object() {value},
System.Globalization.CultureInfo.InvariantCulture)
End Sub
private void PageSetupDialogHidden()
{
Word.Dialog dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];
InvokeHelper(dialog, "PageWidth", "3.3\"");
InvokeHelper(dialog, "PageHeight", "6\"");
InvokeHelper(dialog, "TopMargin", "0.71\"");
InvokeHelper(dialog, "BottomMargin", "0.81\"");
InvokeHelper(dialog, "LeftMargin", "0.66\"");
InvokeHelper(dialog, "RightMargin", "0.66\"");
InvokeHelper(dialog, "HeaderDistance", "0.28\"");
InvokeHelper(dialog, "Orientation", "0");
InvokeHelper(dialog, "DifferentFirstPage", "0");
InvokeHelper(dialog, "FirstPage", "0");
InvokeHelper(dialog, "OtherPages", "0");
// Apply these settings only to the current selection with this line of code:
InvokeHelper(dialog, "ApplyPropsTo", "3");
// Apply the settings.
dialog.Execute();
}
private static void InvokeHelper(Word.Dialog dialog, string member, string value)
{
System.Type dialogType = typeof(Word.Dialog);
// Set the appropriate property of the dialog box.
dialogType.InvokeMember(member,
System.Reflection.BindingFlags.SetProperty |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance,
null, dialog, new object[] { value },
System.Globalization.CultureInfo.InvariantCulture);
}
See Also
Reference
Reflection (C# and Visual Basic)
Concepts
How to: Use Built-In Dialog Boxes in Word
Late Binding in Office Solutions