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
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