共用方式為


HOW TO:以隱藏模式使用 Word 對話方塊

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2003

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

您可以只使用一次方法呼叫來執行複雜的作業,方法是叫用 (Invoke) 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 類別 (Class) 執行這個程式碼。

這個範例會使用 wdDialogFilePageSetup 列舉型別 (Enumeration) 設定多個版面設定屬性,而不需使用者輸入資料。程式碼會使用 Dialog 物件來設定自訂頁面大小。

這個範例需要您在 Visual Basic 程式碼中設定 Option Strict Off。這是必要的,因為網頁設定的特定設定 (例如上方邊界、下方邊界等) 都不是 Dialog 類別的成員。這些都是晚期繫結的 (Late Bound) 屬性,因為這些屬性會在到了執行階段評估 wdDialogFilePageSetup 列舉型別時,才由 Word 以動態方式建立。實際上,這些是在執行階段時建立以對應各個對話方塊控制項的屬性。

注意事項:

您可以將需要搭配 Option Strict Off 執行的程式碼放到不同的類別中。

請參閱

工作

HOW TO:使用 Word 中的內建對話方塊

概念

Word 物件模型概觀