可以在不调用 Display 方法的情况下,使用 Execute 方法悄悄地调用 Microsoft Office Word 2003 中内置的对话框,而不将对话框显示给用户。这使您可以通过一个方法调用来在幕后执行复杂的操作。
对于要编译的代码,必须在代码中设置 Option Strict Off,因为页面设置的特定设置(例如,上边距、下边距等)都不是 Dialog 类的成员。这些都是后期绑定的属性,它们是在计算 wdDialogFilePageSetup 枚举时由 Word 在运行时动态创建的。实际上,它们是在运行时创建的一些用于匹配每个对话框上的控件的属性。
注意 可以把那些需要设置 Option Strict Off 才能运行的代码隔离到一个单独的类中。
使用“如何:在隐藏模式下使用对话框”
- 下面的实例使用 wdDialogPageSetup WdWordDialog 枚举设置多个页面设置属性,不使用任何用户输入。此段代码使用 Dialog 对象来配置自定义页面大小。
' Visual Basic Friend Sub PageSetupDialogHidden() Dim dlg As Word.Dialog dlg = ThisApplication.Dialogs.Item( _ Word.WdWordDialog.wdDialogFilePageSetup) 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 .Orientation = Word.WdOrientation.wdOrientPortrait .DifferentFirstPage = False .HeaderDistance = 0.28 & ControlChars.Quote .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 // C# private void invokeHelper(Word.Dialog dlg, string member, string dlgValue) { Type dlgType = typeof(Word.Dialog); // Set name property of dialog box. dlgType.InvokeMember(member, System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, dlg, new object[] {dlgValue.ToString()}); } private void wordPageSetup() { Word.Dialog dlg = ThisApplication.Dialogs[ Word.WdWordDialog.wdDialogFilePageSetup]; try { invokeHelper(dlg,"PageWidth","3.3\""); invokeHelper(dlg,"TopMargin","0.71\""); invokeHelper(dlg,"BottomMargin","0.81\""); invokeHelper(dlg,"LeftMargin","0.66\""); invokeHelper(dlg,"Orientation","0"); invokeHelper(dlg,"DifferentFirstPage","0"); invokeHelper(dlg,"HeaderDistance","0.28\""); 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(); } catch ( Exception myExp) { MessageBox.Show(myExp.Message); } }