Click here to Skip to main content
14,930,017 members
Articles / Programming Languages / C#
Posted 29 Jun 2005


44 bookmarked

How To Get Properties and Methods in Late Binding COM-Apps Like Excel

Rate me:
Please Sign up or sign in to vote.
3.59/5 (11 votes)
29 Jun 2005CPOL4 min read
The article describes how to find the methods and properties of an COM-Object (like Excel), which is unknown at compilation time (late binding).


If you want to automate Microsoft Excel (or any other COM-Object) from a C#-app, you have two possibilities:

  • Early Binding: If you know, which Excel-version is on the target-machine of your customer (and you are so lucky, that you have this version too, you can do an early binding, i.e. the binding between your app and Excel will be done at compilation-time. You can read more about this here.
  • Late Binding: If you do not know, which version will run on your target-machine, you have to use late binding. The binding to Excel will be done at runtime.

In late binding, there is unfortunately (almost) no support of the methods and properties of the Excel-Object at compile time in Visual Studio .NET. I had to automate Excel with a chart and it was a hard trial and error time to get the appropriate methods and properties. But there is a way to go through (of course, I found it at the very end...) .

Step 1: Write a VBA-script in Excel

Just to be sure, write a VisualBasic Application (VBA) macro in Excel. Then you are sure, that the steps you want to go are really possible. Here you have the support even for one version.

Step 2: Open Excel from your C# App

The technique to do a late binding is described here. First, you have to start the Application out of the registry.

First some definitions:

using System;
using System.Reflection;
public class ExcelCommander
    const int xlAutomatic=-4105;
    const int xlManual   =-4135;
    const int xlUpward    =-4171;
    const int xlWait    =2;
    object oApp;
    object oBook;
    object oBooks;
    object oSheets;
    object oSheet;
    object objRange_Late;
    object oCharts;
    object oChart;
    object oPoints;
    object oPoint;
    object oAxisX;
    object oAxisY;
    object oCursor;
    object[] Parameters;

(If you look for the values of the xlFoo constants: Open the direct-window in VBA and enter the name of the constant. If you are looking for the constant-names itself: seek in the object-browser in VBA.)

Now we do the job: start Excel:

// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
oApp = Activator.CreateInstance(objClassType);

Now we have a reference to the Excel-application.

Step 3: Invoke the Members, Methods and Properties

Before I describe, how to get the members and properties here's the calling technique: it is done in one call:

obj.GetType().InvokeMember(sProperty,BindingFlags, null, obj, oParam );


  • sProperty: Name of the property or method (string)
  • BindingFlags.GetProperty to get a property
  • BindingFlags.SetProperty to set a property
  • BindingFlags.InvokeMethod to call a method
  • obj: Reference to the COM-Object
  • oParam: List of parameters for this call

For convenience, I wrote some wrappers for these calls:

#region private Wrappers
private void SetProperty(object obj,string sProperty,object oValue)
    object[] oParam=new object[1];
    obj.GetType().InvokeMember(sProperty,BindingFlags.SetProperty, null, obj, oParam );
private object GetProperty(object obj,string sProperty,object oValue)
    object[] oParam=new object[1];
    return obj.GetType().InvokeMember
        (sProperty,BindingFlags.GetProperty, null, obj, oParam );
private object GetProperty(object obj,string sProperty,object oValue1,object oValue2)
    object[] oParam=new object[2];
    return obj.GetType().InvokeMember
    (sProperty,BindingFlags.GetProperty, null, obj, oParam );
private object GetProperty(object obj,string sProperty)
    return obj.GetType().InvokeMember
    (sProperty,BindingFlags.GetProperty, null, obj, null );
private object InvokeMethod(object obj,string sProperty,object[] oParam)
    return obj.GetType().InvokeMember
    (sProperty,BindingFlags.InvokeMethod, null, obj, oParam);
private object InvokeMethod(object obj,string sProperty,object oValue)
    object[] oParam=new object[1];
    return obj.GetType().InvokeMember
    (sProperty,BindingFlags.InvokeMethod, null, obj, oParam );

Now we can do the job with little effort.

We open a Workbook:

//Get the workbooks collection.
//Add a new workbook.

As you can see, I opened it with a template. In this way, it is possible to do most of the job (creating a Chart, etc.) already in the template. So we need only import the data values and do some adjustment.

Next, we look for a single Worksheet and do some "Cosmetics":

//Get the worksheets collection.
oSheets = GetProperty(oBook,"Worksheets");
//Get the first worksheet.
oSheet = GetProperty(oSheets,"Item","Temp");
SetProperty(oApp,"UserControl", false);

Due to the fact that the template already has a chart, we can go further and load the references too.

Note: Such properties are only addressable if they are defined in the Excel-sheet. Of course! (I spent a lot of time to learn this...)

//Get the first Diagram.
//Get Axis

Now we can fill in the data. e.g.: Cell(1,1,"Hello World")

public void Cell(int Row,int Col,object Value)
oRange = GetProperty(oSheet,"Range",(char)col+Row.ToString(),Missing.Value);

Or set the Minimum and Maximum of the chart:

public ChartMiniMax(double Xmin,double Xmax,double Ymin,double Ymax)

At least we close the whole business. And here is a problem: the number of parameters depends on the version of Excel. V9.0 uses 9 parameters but V10.0 uses 10 for a "SaveAs" so we have to do some preparing:

//get Excel-version as integer
 case 9:
  Parameters = new Object[9];
 case 10:
  Parameters = new Object[10];
  Parameters = new Object[9];
SetProperty(oApp,"UserControl", true);
Parameters[0] = sFileName;
for (int i=1;i<Parameters.Length;i++)Parameters[i] = Missing.Value;
InvokeMethod(oSheet,"SaveAs",Parameters );
//Return control of Excel to the user.

In my application, I imported some DataLabels too. But for brevity, I omit this part. Up to here, the code is only meant as an example. The main question is still unanswered.

How to Get the Members, Properties and Methods?

Your VBA-macro gives you an idea, which members, methods and properties you need. Now include a reference to your Excel-application:

  • In Visual Studio .NET, Project-Explorer select References
  • Select Add reference
  • Select COM Tab
  • Select Microsoft Excel Open the Object browser <f2>and goto "Excel". There you will find all methods and properties.

For methods (marked with a margenta icon), use the InvokeMethod(...) wrapper.

For properties (white and grey icon), use the GetProperty(...) or SetProperty(...)wrapper.

If you have to get an instance of a collection use the "Item" GetProperty(...,"Item",...). (it seems that you can also use the InvokeMethod(...,"Item",...). In this case, click on the "Item"-item in the object-browser. In the bottom of the window, you see the type of the returned object. Select this object in the object-browser to see its members, properties and methods. In the next step, you have to use the reference to this member.

We saw it already:


A member of ChartObjects is the collection Axes. The first Member of this Axes-collection is the X-axis. You can now use this reference to set or get the properties.

I hope this helps you a little to shorten the way to an Excel solution.


  • 29th June, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) EndoTherm GmbH
Switzerland Switzerland
My first approach to computers was in 1970 a IBM1160, a real desk computer, this means it was as large as a desk and had a cartreader and 32 kByte Memory! But it runs FORTRAN! Ok 'run' is a little too fast <sm>

Later I was very proud on my Comodore CBM8032 32KbRAM+32KbRom (uff!) and 2 (in words 'TWO') floppies. But it was able to run a Basic-Interpreter. (There was a young gay in USA, who invent and developed the garbage collecter for such interpreter, his name was Bill G.)

Unfortunatly this CBM8032 burned away due to some "experiements"...

Ok, later I studied physics and work now since 20 years in the cancer research in Switzerland. My own little company Ariadne-Consulting delivers the login name:


Comments and Discussions

QuestionType.Getproperties() for Word COM object showing inconsistent behavior Pin
Member 856121015-Feb-14 6:13
MemberMember 856121015-Feb-14 6:13 
QuestionHow to cache the method or a property Pin
ExcellentOrg8-Apr-13 19:52
MemberExcellentOrg8-Apr-13 19:52 
AnswerRe: How to cache the method or a property Pin
Ariadne9-Apr-13 3:53
MemberAriadne9-Apr-13 3:53 
GeneralRe: How to cache the method or a property Pin
ExcellentOrg9-Apr-13 10:03
MemberExcellentOrg9-Apr-13 10:03 
GeneralRe: How to cache the method or a property Pin
Ariadne9-Apr-13 10:11
MemberAriadne9-Apr-13 10:11 
QuestionHow to add macro Pin
Mad4runing27-Jan-11 8:35
MemberMad4runing27-Jan-11 8:35 
AnswerRe: How to add macro Pin
Ariadne27-Jan-11 12:04
MemberAriadne27-Jan-11 12:04 
GeneralRe: How to add macro Pin
Mad4runing28-Jan-11 4:09
MemberMad4runing28-Jan-11 4:09 
GeneralRe: How to add macro Pin
Ariadne4-Feb-11 3:18
MemberAriadne4-Feb-11 3:18 
GeneralRe: How to add macro Pin
Mad4runing11-Feb-11 3:38
MemberMad4runing11-Feb-11 3:38 
QuestionCan this code be used to create charts? Or only modify existing charts? Pin
BuckBrown19-Oct-10 11:56
MemberBuckBrown19-Oct-10 11:56 
AnswerRe: Can this code be used to create charts? Or only modify existing charts? Pin
Ariadne19-Oct-10 19:15
MemberAriadne19-Oct-10 19:15 
Questioninteraction between excel and a DLL Pin
TrumanChua10-Dec-09 8:26
MemberTrumanChua10-Dec-09 8:26 
AnswerRe: interaction between excel and a DLL Pin
Ariadne10-Dec-09 21:24
MemberAriadne10-Dec-09 21:24 
QuestionHow do I set the cell's font bold property and cell forecolor and backcolor properties? Pin
Mercuery17-Apr-09 20:39
MemberMercuery17-Apr-09 20:39 
AnswerRe: How do I set the cell's font bold property and cell forecolor and backcolor properties? [modified] Pin
Ariadne20-Apr-09 2:36
MemberAriadne20-Apr-09 2:36 
GeneralRe: How do I set the cell's font bold property and cell forecolor and backcolor properties? Pin
Mercuery20-Apr-09 15:34
MemberMercuery20-Apr-09 15:34 
Question[Message Deleted] Pin
Cracked-Down8-Jan-09 1:14
MemberCracked-Down8-Jan-09 1:14 
AnswerRe: how to bind to the multiple versions of applications installed on the same machine??? Pin
Ariadne8-Jan-09 2:23
MemberAriadne8-Jan-09 2:23 
Question[Message Deleted] Pin
Cracked-Down8-Jan-09 2:46
MemberCracked-Down8-Jan-09 2:46 
AnswerRe: how to bind to the multiple versions of applications installed on the same machine??? Pin
Ariadne8-Jan-09 4:42
MemberAriadne8-Jan-09 4:42 
AnswerRe: how to bind to the multiple versions of applications installed on the same machine??? Pin
Ariadne9-Jan-09 4:11
MemberAriadne9-Jan-09 4:11 
Generalvery grateful for this... Pin
E. del Ayre4-Apr-08 0:26
MemberE. del Ayre4-Apr-08 0:26 
AnswerRe: very grateful for this... Pin
Ariadne4-Apr-08 3:34
MemberAriadne4-Apr-08 3:34 
QuestionLate Binding Reflection? Pin
Richard A Hunt18-Feb-08 3:20
MemberRichard A Hunt18-Feb-08 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.