Share via


How to: Programmatically use Word dialog boxes in hidden mode

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 VSTO Add-in projects for Word. 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. 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

See also