Aracılığıyla paylaş


How to: Use Word Dialog Boxes in Hidden Mode

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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(Object) method.

Example

Friend Sub PageSetupDialogHidden()
    Dim dlg 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 dlg
        .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 
End Sub
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

Run this code from the ThisDocument or ThisAddIn class in your Visual Studio Tools for Office project.

This example uses the wdDialogFilePageSetup enumeration to set multiple page setup properties with no user input. 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, because they are 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.

Note

You can factor code that needs to run with Option Strict Off into a separate class.

See Also

Tasks

How to: Use Built-In Dialog Boxes in Word

Concepts

Word Object Model Overview