How to: Automate Objects Using Managed Code
When you write managed code, by default you must specify an object's type. However, many Office object model methods and properties return the type Object, because they have the capability to return several different types. When the object is returned, you must explicitly convert (in Visual Basic) or cast (in C#) the object to the correct type. Converting or casting the object enables IntelliSense for the object in the code editor.
For information on conversion in Visual Basic, see Implicit and Explicit Conversions and CType Function. For information on casting in C#, see () Operator.
The following procedure is an example of casting an object returned by Office to a specific type for use in managed code.
To cast an object to a specific type
-
Add a NamedRange control to cell A1.
-
Cast the object returned from the column width of the named range to a double and assign it to a variable.
-
Display the column width of the NamedRange control in a dialog box.
See Also
Tasks
How to: Automate Office Applications By Using Primary Interop AssembliesConcepts
Managed Code and Office Programming with Visual StudioUnderstanding Optional Parameters in COM Interop
Other Resources
Programming Office ApplicationsControls in Microsoft Office Projects
Office Development Samples and Walkthroughs