Office 方案中的晚期繫結

Office 應用程式物件模型中部分型別所提供的功能,也可以透過晚期繫結取得。 例如,有些方法和屬性可能會根據 Office 應用程式內容傳回不同型別的物件,而有些型別可能會公開不同內容中的不同方法或屬性。

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

關閉 Option Strict 的 Visual Basic 專案和目標為 .NET Framework 4 的 Visual C# 專案,可以直接與使用這些晚期繫結功能的型別搭配運作。 開啟 Option Strict 的 Visual Basic 專案和目標為 .NET Framework 3.5 的 Visual C# 專案,則必須使用轉型或反映才能使用這些型別。

物件傳回值的隱含和明確轉型

Microsoft Office 主要 Interop 組件 (PIA) 中的許多方法和屬性會傳回 Object 值,因為它們可以傳回多種不同的物件型別。 例如,ActiveSheet 屬性的傳回值可以是 WorksheetChart 物件 (視使用中工作表而定),因此會傳回 Object

方法或屬性傳回 Object 時,您必須隱含地將物件轉換 (在 Visual Basic 中) 或轉型 (在 C# 中) 為已開啟 Option Strict 之 Visual Basic 專案和目標為 .NET Framework 3.5 之 Visual C# 專案中的正確型別。 您不需要明確轉型已關閉 Option Strict 之 Visual Basic 專案和目標為 .NET Framework 4 之 Visual C# 專案中的 Object 傳回值。

在大部分情況下,參考文件會列出傳回 Object 之成員的可能傳回值型別。 轉換或轉型物件會在程式碼編輯器中對物件啟用 IntelliSense。

如需在 Visual Basic 中轉換的詳細資訊,請參閱隱含和明確轉換 (Visual Basic)CType 函式 (Visual Basic)。 如需 Visual C# 中轉型的詳細資訊,請參閱轉型和型別轉換 (C# 程式設計手冊)() 運算子 (C# 參考)

範例

下列程式碼範例示範如何將物件轉型為已開啟 Option Strict 之 Visual Basic 專案或目標為 .NET Framework 3.5 之 Visual C# 專案中的特定型別。 在這些類型的專案中,您必須將 Cells 屬性明確轉型為 Range。 這個範例需要名稱為 Sheet1 之工作表類別的文件層級 Excel 專案。

Dim castRange As Excel.Range = CType(Globals.Sheet1.Cells(1, 1), Excel.Range)
Excel.Range castRange = (Excel.Range)Globals.Sheet1.Cells[1, 1];

下列程式碼範例示範如何將物件隱含地轉型為已關閉 Option Strict 之 Visual Basic 專案和目標為 .NET Framework 4 之 Visual C# 專案中的特定型別。 在這些類型的專案中,會將 Cells 屬性隱含地轉型為 Range。 這個範例需要名稱為 Sheet1 之工作表類別的文件層級 Excel 專案。

Dim dynamicRange As Excel.Range = Globals.Sheet1.Cells(1, 1)
Excel.Range dynamicRange = Globals.Sheet1.Cells[1, 1];

存取只能透過晚期繫結使用的成員

Office PIA 中的部分屬性和方法只能透過晚期繫結才能使用。 在關閉 Option Strict 的 Visual Basic 專案或目標為 .NET Framework 4 的 Visual C# 專案中,您可以在這些語言中使用晚期繫結功能,以存取晚期繫結的成員。 在開啟 Option Strict 的 Visual Basic 專案或目標為 .NET Framework 3.5 的 Visual C# 專案,您必須使用反映才能存取這些成員。

範例

下列程式碼範例示範如何存取已關閉 Option Strict 之 Visual Basic 專案或目標為 .NET Framework 4 之 Visual C# 專案中的晚期繫結的成員。 這個範例存取 Word 中 [開啟舊檔] 對話方塊的晚期繫結 Name 屬性。 若要使用這個範例,請從 Word 專案中的 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);

下列程式碼範例示範如何使用反映在已開啟 Option Strict 之 Visual Basic 專案或目標為 .NET Framework 3.5 之 Visual C# 專案中完成相同的工作。

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))
Word.Dialog dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
System.Type dialogType = typeof(Word.Dialog);

// Set the Name property of the dialog box.
dialogType.InvokeMember("Name", 
    System.Reflection.BindingFlags.SetProperty | 
        System.Reflection.BindingFlags.Public | 
        System.Reflection.BindingFlags.Instance,
    null, dialog, new object[] { "Testing" },
    System.Globalization.CultureInfo.InvariantCulture);

// Display the dialog box.
dialog.Show(ref missing); 

// Show the Name property.
MessageBox.Show(dialogType.InvokeMember("Name",
    System.Reflection.BindingFlags.GetProperty |
        System.Reflection.BindingFlags.Public |
        System.Reflection.BindingFlags.Instance,
    null, dialog, null,
    System.Globalization.CultureInfo.InvariantCulture).ToString());

請參閱

參考

Option Strict 陳述式

反映 (C# 和 Visual Basic)

概念

撰寫 Office 方案中的程式碼

Office 方案中的選擇性參數

其他資源

使用動態型別 (C# 程式設計手冊)

設計和建立 Office 方案