473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help with talking to Excel from C#

Hi all, I am trying to convert some old VB6 code to .NET.

Take this first section (there is more, but hey-ho...)
Dim XLApp As Object
Dim XLSheet
Dim XLBook
Dim HeaderItem As String
Dim ColCount As Integer
ColCount = 1
Set XLApp = CreateObject("EXCEL.Application")
XLApp.Visible = False
XLApp.Workbooks.Open (ExcelFile)

Now, after googling, I have found I can either add a reference to Excel, or
I can use the "GetTypeFromProgID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobject" method didn't seem to care.

So far, I have this:

//Create Excel object
Object XLApp;
Object XLWorkBook;
Object XLSheet;
Object XLBook;
String HeaderItem;
Int16 ColCount = 1;
Type ExcelType = Type.GetTypeFromProgID("EXCEL.Application");
lstExcelProgress.Items.Add("Creating Excel instance...");
try
{
XLApp = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible",
System.Reflection.BindingFlags.SetProperty, null, XLApp, new object[] {
false });
lstExcelProgress.Items.Add("Trying to open the workbook...");
try
{
XLWorkBook = ExcelType.InvokeMember("Workbooks.Open",
System.Reflection.BindingFlags.InvokeMethod, null, XLApp, new object[]
{ExcelFile});
}
catch (Exception wer)
{
//handle error }
}
catch (Exception er)
{
//handle error
}

Now, the first bit works OK where I create my Excel instance, but the second
bit (opening the workbook) is failing, the error returned being Exception
from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME).

I assume it doesn't like Workbooks.Open as a method to run, but I'm not sure
how else to do it? Any ideas?

James.

Dec 4 '06 #1
2 6497

james wrote:
Hi all, I am trying to convert some old VB6 code to .NET.

Take this first section (there is more, but hey-ho...)
Dim XLApp As Object
Dim XLSheet
Dim XLBook
Dim HeaderItem As String
Dim ColCount As Integer
ColCount = 1
Set XLApp = CreateObject("EXCEL.Application")
XLApp.Visible = False
XLApp.Workbooks.Open (ExcelFile)

Now, after googling, I have found I can either add a reference to Excel, or
I can use the "GetTypeFromProgID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobject" method didn't seem to care.

So far, I have this:

//Create Excel object
Object XLApp;
Object XLWorkBook;
Object XLSheet;
Object XLBook;
String HeaderItem;
Int16 ColCount = 1;
Type ExcelType = Type.GetTypeFromProgID("EXCEL.Application");
lstExcelProgress.Items.Add("Creating Excel instance...");
try
{
XLApp = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible",
System.Reflection.BindingFlags.SetProperty, null, XLApp, new object[] {
false });
lstExcelProgress.Items.Add("Trying to open the workbook...");
try
{
XLWorkBook = ExcelType.InvokeMember("Workbooks.Open",
System.Reflection.BindingFlags.InvokeMethod, null, XLApp, new object[]
{ExcelFile});
}
catch (Exception wer)
{
//handle error }
}
catch (Exception er)
{
//handle error
}

Now, the first bit works OK where I create my Excel instance, but the second
bit (opening the workbook) is failing, the error returned being Exception
from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME).

I assume it doesn't like Workbooks.Open as a method to run, but I'm not sure
how else to do it? Any ideas?
Just off the cuff, remember that while VB6 has the concept of "optional
parameters", which it uses with gay abandon, C# has no such concept.
When you call Office apps from C#, you must pass an argument for every
method parameter. If want to omit an argument then you have to pass
Type.Missing as the argument value.

See the following for more information.

http://msdn2.microsoft.com/en-gb/lib...ffcsharp_link3

Again, I don't know whether this is your case: perhaps Workbooks.Open
takes just one argument and you supplied it.

The other thing I notice is that you're passing "Workbooks.Open" as the
method name. I'm not sure that this will work... at least I've never
used Reflection that way. I would think that you would first have to
get a Type for Workbooks, and then Invoke "Open" on _that_ type, rather
than trying to do it all in one go.

Dec 4 '06 #2

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Just off the cuff, remember that while VB6 has the concept of "optional
parameters", which it uses with gay abandon, C# has no such concept.
When you call Office apps from C#, you must pass an argument for every
method parameter. If want to omit an argument then you have to pass
Type.Missing as the argument value.
OK - there are actually 14 "optional" parameters for that that I can see in
the version of Excel I am using...

See the following for more information.

http://msdn2.microsoft.com/en-gb/lib...ffcsharp_link3

Again, I don't know whether this is your case: perhaps Workbooks.Open
takes just one argument and you supplied it.

The other thing I notice is that you're passing "Workbooks.Open" as the
method name. I'm not sure that this will work... at least I've never
used Reflection that way. I would think that you would first have to
get a Type for Workbooks, and then Invoke "Open" on _that_ type, rather
than trying to do it all in one go.
I think that's more likely - I tried adding 14 Type.Missing's to my command
and I get the same error... the "not found" bit suggests its perhaps not
even finding the method name, leading me to think your latter suggestion is
the one to try.
Thanks,
James

Dec 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc | last post by:
Hi all, I am trying to write an application where I need the ability to open an Excel spreadsheet and do basic read/write, insert rows, and hide/unhide rows. Using win32com I have been able to...
34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
4
by: Derek Richards | last post by:
The following syntax is what I found under the Help in Excel's VBA Editor (you may refer to it for the details of each parameter). i specified to open in Write mode (Read Only is false), it...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
0
by: fjlaga | last post by:
I wrote an Excel Add-In in VB.NET using Visual Studio .NET 2003 and the ..NET Framework version 1.1.4322 on my previous machine. This machine had MS Office 10 installed on it. With this...
1
by: Coleen | last post by:
To export data from a datagrid to an excel file. I am using VB.net/ASP.net v 2003, and not connecting to an SQL database. I have an existing datagrid that I want to get the data from and download...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.