如何:在隐藏模式下使用 Word 对话框

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2003

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

在 Microsoft Office Word 中,可以使用一个方法调用在不向用户显示的情况下调用内置对话框,从而执行复杂的操作。通过使用 Dialog 对象的 Execute 方法,而不调用 Display 方法,就可以执行此操作。

示例

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

编译代码

请从 Visual Studio Tools for Office 项目内的 ThisDocument 或 ThisAddIn 类中运行此代码。

此示例使用 wdDialogFilePageSetup 枚举设置多个页面设置属性,而不使用任何用户输入。此段代码使用 Dialog 对象来配置自定义页面大小。

此示例要求您在 Visual Basic 代码中设置 Option Strict Off。因为页面设置的具体设置(如上边距、下边距等)都不是 Dialog 类的成员,故而有必要这样做。这些都是后期绑定的属性,因为它们是在计算 wdDialogFilePageSetup 枚举时由 Word 在运行时动态创建的。实际上,它们是在运行时创建的一些用于匹配每个对话框上的控件的属性。

bhy5dcs0.alert_note(zh-cn,VS.90).gif说明:

可以将需要使用 Option Strict Off 运行的代码包括在一个单独的类中。

请参见

任务

如何:使用 Word 中的内置对话框

概念

Word 对象模型概述