2

Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:

objExcel = CreateObject("Excel.Application") 

Now I want to use the InvokeMember method, but I don't know all the members of excel I can invoke. For example, I know I can call it like this: InvokeMember("Close",... in order to close excel, but where can I find list of all the members I can invoke and what each one of them does?

1 Answer 1

7

Late-bound

Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:

If you must use late-binding, using c# 4.0's dynamic keyword is a whole lot easier than InvokeMember, though it won't show you what methods you can invoke ahead of time.

Check out the following code that uses late-binding via the dynamic keyword. Notice how Visual Studio allows me to type in any old thing. Though auto-complete for the final members aren't available, it does show members for items I've used already. I won't know until runtime whether I got it right (such is the limitation of late-binding this way).

enter image description here

C# now supports dynamic late-binding. The language has always been strongly typed, and it continues to be so in version 4.0. Microsoft believes this makes C# easy to use, fast and suitable for all the work .NET programmers are putting it to. But there are times when you need to communicate with systems not based on .NET....The dynamic keyword in C# is a response to dealing with the hassles of these other approaches Tell me more

...and more specifically:

The COM interop scenario that the C# team specifically targeted in the C# 4 release was programming against Microsoft Office applications, such as Word and Excel. The intent was to make this task as easy and natural in C# as it always was in Visual Basic. Tell me more...

Early-bound

OP:

Now I want to use the InvokeMember method, but I don't know all the members of excel I can invoke

Though late binding is fine, even with dynamic, I like early binding. To get a list of methods, it's much easier and type-safe to use early binding via adding Microsoft.Office.Interop.Excel to your project.

Early binding:

var application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = true;
application.ShowWindowsInTaskbar = true;

Here it is in VS:

enter image description here

C# 4 Goodness

c# 4 brings with it some stuff you'll only see when dealing with COM, like indexed properties - something not possible in c# types.

You can’t define types in C# that have indexed properties, but you can use them provided you’re doing so on a COM type more

Some smaller language features in C# 4.0 are supported only when writing code against a COM interop API

e.g.

ws.Range["A1", "B3"].Value = 123; 

...is a whole lot easier than pre-c# 4:

ws.get_Range("A1", "B3").Value2 = 123;

C# 4.0 supports indexed properties on COM interop types. You can’t define types in C# that have indexed properties, but you can use them provided you’re doing so on a COM type more...

Tell me more

enter image description here

3
  • Thank you very much for this input!
    – Itay.B
    Commented Dec 10, 2015 at 7:42
  • 1
    From what I can gather, I believe you could: 1. Set a reference to the Excel Library within your IDE (effectively early binding) or 2. Open an excel session and within the VBIDE type some object and see what it has to offer. 3. Look at Microsoft's online documentation for your object. BTW, you should be able to use the No PIA option which embeds and cleverly maps your specific functions. So maybe early binding may be more feasible now than in the past...maybe.
    – PaulG
    Commented Dec 10, 2015 at 9:16
  • Yes that's right, they've done away with IA and PIA. See the article in the July 2010 link
    – user585968
    Commented Dec 10, 2015 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.