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.
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.
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.
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); }