Dela via


Late Binding in Office Solutions

Some types in the object models of Office applications provide functionality that is available through late-binding features. For example, some methods and properties can return different types of objects depending on the context of the Office application, and some types can expose different methods or properties in different contexts.

Applies to: The information in this topic applies to document-level projects and application-level projects for Microsoft Office 2010 and the 2007 Microsoft Office system. For more information, see Features Available by Office Application and Project Type.

Visual Basic projects where Option Strict is off and Visual C# projects that target the .NET Framework 4 can work directly with types that employ these late-binding features. Visual Basic projects where Option Strict is on and Visual C# projects that target the .NET Framework 3.5 must use casting or reflection to use these types.

Implicit and Explicit Casting of Object Return Values

Many methods and properties in the Microsoft Office primary interop assemblies (PIAs) return Object values, because they can return several different types of objects. For example, the ActiveSheet property returns an Object because its return value can be a Worksheet or Chart object, depending on what the active sheet is.

When a method or property returns an Object, you must explicitly convert (in Visual Basic) or cast (in C#) the object to the correct type in Visual Basic projects where Option Strict is on and in Visual C# projects that target the .NET Framework 3.5. You do not have to explicitly cast Object return values in Visual Basic projects where Option Strict is off and in Visual C# projects that target the .NET Framework 4.

In most cases, the reference documentation lists the possible types of the return value for a member that returns an Object. Converting or casting the object enables IntelliSense for the object in the Code Editor.

For information about conversion in Visual Basic, see Implicit and Explicit Conversions (Visual Basic) and CType Function (Visual Basic). For information about casting in Visual C#, see Casting and Type Conversions (C# Programming Guide) and () Operator (C# Reference).

Examples

The following code example demonstrates how to cast an object to a specific type in a Visual Basic project where Option Strict is on or in a Visual C# project that targets the .NET Framework 3.5. In these types of projects, you must explicitly cast the Cells property to a Range. This example requires a document-level Excel project with a worksheet class named Sheet1.

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

The following code example demonstrates how to implicitly cast an object to a specific type in a Visual Basic project where Option Strict is off or in a Visual C# project that targets the .NET Framework 4. In these types of projects, the Cells property is implicitly cast to a Range. This example requires a document-level Excel project with a worksheet class named Sheet1.

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

Accessing Members That Are Available Only Through Late Binding

Some properties and methods in the Office PIAs are available only through late binding. In Visual Basic projects where Option Strict is off or in Visual C# projects that target the .NET Framework 4, you can use the late binding features in these languages to access late-bound members. In Visual Basic projects where Option Strict is on or in Visual C# projects that target the .NET Framework 3.5, you must use reflection to access these members.

Examples

The following code example demonstrates how to access late-bound members in a Visual Basic project where Option Strict is off or in a Visual C# project that targets the .NET Framework 4. This example accesses the late-bound Name property of the File Open dialog box in Word. To use this example, run it from the ThisDocument or ThisAddIn class in a Word project.

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);

The following code example demonstrates how to use reflection to accomplish the same task in a Visual Basic project where Option Strict is on or in a Visual C# project that targets the .NET Framework 3.5.

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());

See Also

Reference

Option Strict Statement

Reflection (C# and Visual Basic)

Concepts

Writing Code in Office Solutions

Optional Parameters in Office Solutions

Other Resources

Using Type dynamic (C# Programming Guide)

Designing and Creating Office Solutions