在 Microsoft Office Word 中,可以使用一个方法调用在不向用户显示的情况下调用内置对话框,从而执行复杂的操作。 通过使用 Dialog 对象的 Execute 方法,而不调用 Display 方法,就可以执行此操作。
**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。
示例
下面的代码示例演示如何在隐藏模式下使用**“页面设置”**对话框在无用户输入的情况下设置多个页面设置属性。 这些示例使用 Dialog 对象来配置自定义页面大小。 页面设置的具体设置(例如上边距、下边距等)以 Dialog 对象的后期绑定属性的形式提供。 这些属性是由 Word 在运行时动态创建的。
下面的示例演示如何在 Option Strict 处于关闭状态的 Visual Basic 项目和面向 .NET Framework 4 的 Visual C# 项目中执行此任务。 在这些项目中,您可以使用 Visual Basic 和 Visual C# 编译器中的后期绑定功能。 若要使用此示例,请从项目内的 ThisDocument 或 ThisAddIn 类中运行此示例。
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();
下面的示例演示如何在 Option Strict 处于打开状态的 Visual Basic 项目和面向 .NET Framework 3.5 的 Visual C# 项目中执行此任务。 在这些项目中,您必须使用反射来访问后期绑定的属性。 若要使用此示例,请从项目内的 ThisDocument 或 ThisAddIn 类中运行此示例。
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
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);
}