共用方式為


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

您可以只使用一次方法呼叫來執行複雜的作業,方法是叫用 (Invoke) 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);
}

請參閱

工作

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

Office 方案中的晚期繫結

參考

反映 (C# 和 Visual Basic)

其他資源

Word 物件模型概觀