Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

Setting a value in an Excel cell using C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Emmanuel - 30 Nov 2004 16:20 GMT
Hi,

I 've managed to run successfully the example provided by the Microsoft
Q302901 - "How To Build an Office COM Add-in by Using Visual C# .NET"
article http://support.microsoft.com/default.aspx?scid=kb;en-us;302901

I want to change the button_click event code so that, when the button is
pressed, a value is written at the cell A1 of the active sheet.

I tryied the following code:

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
   // Get the active worksheet.
  object sheet = applicationObject.GetType().InvokeMember("ActiveSheet",
BindingFlags.GetProperty, null, applicationObject, null);
   // Get range A1
  object range = sheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, sheet, new object[] {"A1", Missing.Value});
   // Set the value "Hello world" to that range.
  range.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,
range, new object[] {"Hello world"});
}

The above code works fine for the first two invocations. But when the third
invocation that sets the value of the range is run, the application throws
an exception. If I catch the exception inside a try {} catch{} I get the
message:
"Exception has been thrown by the target of an invocation." in the
Exception.Message property
and
"mscorlib" in the Exception.Source property.

Does anyone know why I cannot set a property although I can get the
properties right ?

Thanks

Emmanuel
Eric - 01 Dec 2004 00:34 GMT
http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757

http://msdn.microsoft.com/office/understanding/excel/codesamples/default.aspx?pu
ll=/library/en-us/odc_vsto2003_ta/html/excelobj.asp


Regards,
Eric

> Hi,
>
[quoted text clipped - 35 lines]
>
> Emmanuel
Emmanuel - 01 Dec 2004 13:23 GMT
Thanks Eric for the document links.

Emmanuel

> http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757
>
[quoted text clipped - 46 lines]
>>
>> Emmanuel
"Peter Huang" [MSFT] - 01 Dec 2004 03:03 GMT
Hi

It is strange that I can not reproduce the problem on my side.
Here is a link about how to set the value of range within C# including
early-binding and late-binding.
Binding for Office automation servers with Visual C# .NET
http://support.microsoft.com/kb/302902/EN-US/

Also why you did not use early-binding as the KB article below?
Do you have any concern?

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

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

Emmanuel - 01 Dec 2004 13:35 GMT
Hi Peter,

I will continue this thread only. Sorry for the mess.

I' am using Microsoft Excel 2003 in Windows XP and Visual Studio 2003.

I checked C:\Windows\Assembly and found two assemblies both named "office":
The first one is version 7.0.3300.0 and the other one is version 11.0.0.0.

Then I found 10 more assemblies called:
Microsoft.Office.Interop.Excel, (...Word, ...Access, etc)
All this assemblies are in version 11.0.0.0

I also checked the installation of Office 2003 and I see that ".NET
Programmability support" is installed for every office application.

So is this installation of PIAs correct ?

Thanks
Emmanuel

> Hi
>
[quoted text clipped - 15 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 02 Dec 2004 01:59 GMT
Hi

Yes, it seems that the office 2003 PIA has been installed.
Have you tried to set the property using early binding?
Did that work for you?
Also I think you may try to follow the KB article to write a simple test
application to see if there is any problem?

Please perform the test so that we can isolate the problem, thanks.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

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

Emmanuel - 02 Dec 2004 08:31 GMT
Hi Peter,

I performed the test with the KB article 302902 without changing a single
line. The only thing that might be different from the article is that when I
am
adding the reference, from the COM tab I select: "Microsoft Excel 11.0
Object Library" that has TypeLib Version 1.5.
When I select this reference, another reference with the name "VBIDE"
appears in the list of my project's references.

I execute the sample code and when I press the button I get the message:
"Error: Old format or invalid type library. Line:
Microsoft.Office.Interop.Excel

Do you have any idea what is happenning here ?

Thank you
Emmanuel
> Hi
>
[quoted text clipped - 14 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Emmanuel - 02 Dec 2004 11:03 GMT
Hi Peter,

I found the solution to my problem. There was nothing wrong with my code,
not even with the PIA stuff.

The only problem was that I my Regional Settings were not "en-US" but
something else.

This problem was due to a Microsoft BUG (check KB article 320369).
As a work around, I borrowed some code from the above article:

   // Save cultural settings in oldCI var and set the settings to "en-US"
   System.Globalization.CultureInfo oldCI =
System.Threading.Thread.CurrentThread.CurrentCulture;
   System.Threading.Thread.CurrentThread.CurrentCulture =  new
System.Globalization.CultureInfo("en-US");

   ... call methods and properties using interop

   // Restore the original cultural settings.
   System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

The funny thing is that this work around works for both the early and late
binding. The problem with the
late binding was that I did not receive the "Old format or invalid type
library" message.

Thank you all
I consider this thread closed.

Emmanuel

> Hi Peter,
>
[quoted text clipped - 32 lines]
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
"Peter Huang" [MSFT] - 03 Dec 2004 01:27 GMT
Hi

I am glad that you have found the solution.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

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

Emmanuel - 01 Dec 2004 16:20 GMT
Peter,

I also tyied to use early binding but this also has failed, having the same
behaviour:

I can get the name of the active worksheet (for example) but I cannot write
a value to a cell.

Please help

Thanks
Emmanuel
> Hi
>
[quoted text clipped - 15 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 02 Dec 2004 02:02 GMT
Hi

Is there any problem with the excel workbook(e.g. open as read-only ....)
Have you tried to open a new excel workbook to have a try?

Also I think you may try to do the same job on another job to see if there
it works.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.