Compartilhar via


Late binding in Office solutions

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 VSTO Add-in projects. 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];

Access 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