共用方式為


HOW TO:以程式設計方式使用 Word 中的內建對話方塊

在使用 Microsoft Office Word 的時候,您有時需要顯示對話方塊讓使用者進行輸入。雖然您可以自行建立,不過也可以使用 Word 中的內建對話方塊,這些對話方塊是公開在 Application 物件的 Dialogs 集合中。您可以存取超過 200 種以上以列舉型別 (Enumeration) 表示的內建對話方塊。

**適用於:**本主題中的資訊適用於 Word 2013 和 Word 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

顯示對話方塊

若要顯示對話方塊,請使用其中一個 WdWordDialog 列舉值來建立 Dialog 物件,以表示您要顯示的對話方塊。然後,呼叫 Dialog 物件的 Show 方法。

下列程式碼範例示範如何顯示 [開啟舊檔] 對話方塊。若要使用這個範例,請從專案中的 ThisDocument 或 ThisAddIn 類別中執行。

Dim dlg As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFileOpen)
dlg.Show()
Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
dlg.Show();

ahzbkf8e.collapse_all(zh-tw,VS.110).gif存取透過晚期繫結使用的對話方塊成員

Word 中對話方塊的部分屬性和方法只能透過晚期繫結才能使用。在 Visual Basic 專案 Option Strict 位置開啟,您必須使用反映才能存取這些成員。如需詳細資訊,請參閱Office 方案中的晚期繫結

下列程式碼範例在 Option Strict 或在開啟以 .NET Framework 4 或 .NET Framework 4.5的 Visual Basic 專案示範如何使用 [開啟的檔案。] 對話方塊的 Name 屬性。若要使用這個範例,請從專案中的 ThisDocument 或 ThisAddIn 類別中執行。

Private Sub TestDynamicDialog()
    Dim dialog As Word.Dialog = Application.Dialogs(Word.WdWordDialog.wdDialogFileOpen)
    dialog.Name = "Testing"
    dialog.Show()
    MessageBox.Show(dialog.Name)
End Sub
dynamic dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
dialog.Name = "Testing";
dialog.Show();
MessageBox.Show(dialog.Name);

下列程式碼範例示範如何使用反映來存取 [開啟的檔案。] 對話方塊的 Name 屬性在 Option Strict 開啟的 Visual Basic 專案的。若要使用這個範例,請從專案中的 ThisDocument 或 ThisAddIn 類別中執行。

Dim dlg As Word.Dialog = Application.Dialogs(Word.WdWordDialog.wdDialogFileOpen)
Dim dlgType As Type = GetType(Word.Dialog)

' Set the Name property of the dialog box.
dlgType.InvokeMember("Name", _
    Reflection.BindingFlags.SetProperty Or _
        Reflection.BindingFlags.Public Or _
        Reflection.BindingFlags.Instance, _
    Nothing, dlg, New Object() {"Testing"}, _
    System.Globalization.CultureInfo.InvariantCulture)

' Display the dialog box.
dlg.Show()

' Show the Name property.
MessageBox.Show(dlgType.InvokeMember("Name", _
    Reflection.BindingFlags.GetProperty Or _
        Reflection.BindingFlags.Public Or _
        Reflection.BindingFlags.Instance, _
    Nothing, dlg, Nothing, _
    System.Globalization.CultureInfo.InvariantCulture))

請參閱

參考

Option Strict 陳述式

反映 (C# 和 Visual Basic)

概念

HOW TO:以程式設計方式在隱藏模式中使用 Word 對話方塊

Office 方案中的選擇性參數

其他資源

Word 物件模型概觀