This documentation is archived and is not being maintained.

How to: Use Word Dialog Boxes in Hidden Mode

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

You can perform complex operations behind the scenes with one method call by silently invoking the built-in dialog boxes in Microsoft Office Word 2003 without displaying them to the user. You can do this using the Execute method of the Dialog object without calling the Display method.

Example

private void PageSetupDialogHidden() 
{ 
    Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];

    invokeHelper(dlg,"PageWidth","3.3\"");
    invokeHelper(dlg,"PageHeight","6\"");
    invokeHelper(dlg,"TopMargin","0.71\"");
    invokeHelper(dlg,"BottomMargin","0.81\"");
    invokeHelper(dlg,"LeftMargin","0.66\"");
    invokeHelper(dlg,"RightMargin","0.66\"");
    invokeHelper(dlg,"HeaderDistance","0.28\"");
    invokeHelper(dlg,"Orientation","0");
    invokeHelper(dlg,"DifferentFirstPage","0");
    invokeHelper(dlg,"FirstPage","0");
    invokeHelper(dlg,"OtherPages","0");

    // Apply these settings only to the current selection with this line of code:
    invokeHelper(dlg,"ApplyPropsTo","3"); 

    // Apply the settings.
    dlg.Execute(); 
}

private static void invokeHelper(Word.Dialog dlg, string member, string value)
{
    System.Type dlgType = typeof(Word.Dialog);

    // Set the appropriate property of the dialog box.
    dlgType.InvokeMember(member,
        System.Reflection.BindingFlags.SetProperty |
            System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance,
        null, dlg, new object[] {value},
        System.Globalization.CultureInfo.InvariantCulture);
}

Compiling the Code

This example uses the wdDialogFilePageSetup enumeration to set multiple page setup properties using no user input whatsoever. The code uses a Dialog object to configure a custom page size. This example requires that you:

  • Set Option Strict Off in your Visual Basic code. This is necessary because none of the specific settings for page setup, such as the top margin, bottom margin, and so on, are members of the Dialog class. These are properties that are late bound, being dynamically created by Word at run time when the wdDialogFilePageSetup enumeration is evaluated. In reality, they are properties created at run time to match controls on each individual dialog box.

    NoteNote

    You can segregate code that needs to run with Option Strict Off in a separate class.

See Also

Show: