Export (0) Print
Expand All

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.

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.


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.


Excel.Range dynamicRange = Globals.Sheet1.Cells[1, 1];


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.


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.


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


Community Additions

ADD
Show:
© 2014 Microsoft