Writting to Excel using a COM Add-In and C#

E

Emmanuel

Hi everybody,

I have already opened a thread with the same question but since I got no
answers I will try to rephrase my question and be more specific.

- I have already build a COM Add-In following the Microsoft Q302901 article.
- This COM Add-in adds a button at the "Standard" Excel toolbar.
- When I press the button I want a value to be inserted on the current cell
of the active sheet.
- I want the add-in to user late-binding when talking to Excel, so no Excel
references are done in the project.

I am trying to achieve the above behaviour, by following the method bellow:

1. In the method OnConnection(), the application object, that comes as a
parameter, is stored on a private variable called applicationObject.
2. In the button_Click() event handling method, I inserted the following
code (using parts of the Microsoft article Q302902):

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
// Get the active sheet.
object sheet = applicationObject.GetType().InvokeMember("ActiveSheet",
BindingFlags.GetProperty, null, applicationObject, null);

// Get the range.
object range = sheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, sheet, new object[] {"A1", Missing.Value});

// Write the value
range.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,
range, new object[] {"Hello world!"});
}

My problem is that although the first two invocations in the method (sheet
and range) work fine, the third one, that writes the value into the range,
throws an exception.

Can anyone tell me what is going wrong here ?

Why I can read but not write to properties ?

Please help...

Thanks

Emmanuel
 
P

Peter Huang [MSFT]

Hi

I reviewed the thread and find that there is a similar issue in the
newsgroup below.Now I have replied to you, you may go and take a look.
Subject: Setting a value in an Excel cell using C#
Newsgroups: microsoft.public.dotnet.framework.interop

BTW, which version of excel are you using?
If you are using office XP or office 2003 have you installed the PIA.
Office 2003 PIA is installed with office 2003 install program, office xp
pia can be reach via the link below.
Working with the Office XP Primary Interop Assemblies
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/htm
l/odc_oxppias.asp

Also you may tried on another machine or tried to use eary-binding to see
if the problem persists.

If you still have any concern please reply in that thread and we will
follow up you in that thread,.

Thanks.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top