Compartilhar 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 Office 2013 and Office 2010. 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 or the .NET Framework 4.5 can work directly with types that employ these late-binding features.

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 a Object, you must explicitly convert (in Visual Basic) the object to the correct type in Visual Basic projects where Option Strict is on. You do not have to explicitly cast Object return values in Visual Basic projects where Option Strict is off.

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

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. In this type of project, 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)

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 or the .NET Framework 4.5, you can use the late binding features in these languages to access late-bound members. In Visual Basic projects where Option Strict is on, 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.

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

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