473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("E XCEL.Applicatio n")
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 "GetTypeFromPro gID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobje ct" 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.GetTypeFro mProgID("EXCEL. Application");
lstExcelProgres s.Items.Add("Cr eating Excel instance...");
try
{
XLApp = Activator.Creat eInstance(Excel Type);
ExcelType.Invok eMember("Visibl e",
System.Reflecti on.BindingFlags .SetProperty, null, XLApp, new object[] {
false });
lstExcelProgres s.Items.Add("Tr ying to open the workbook...");
try
{
XLWorkBook = ExcelType.Invok eMember("Workbo oks.Open",
System.Reflecti on.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_UNKNOWN NAME).

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 6534

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("E XCEL.Applicatio n")
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 "GetTypeFromPro gID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobje ct" 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.GetTypeFro mProgID("EXCEL. Application");
lstExcelProgres s.Items.Add("Cr eating Excel instance...");
try
{
XLApp = Activator.Creat eInstance(Excel Type);
ExcelType.Invok eMember("Visibl e",
System.Reflecti on.BindingFlags .SetProperty, null, XLApp, new object[] {
false });
lstExcelProgres s.Items.Add("Tr ying to open the workbook...");
try
{
XLWorkBook = ExcelType.Invok eMember("Workbo oks.Open",
System.Reflecti on.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_UNKNOWN NAME).

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*******@cana da.comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.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
7192
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 get the basics down as well as some examples displaying how to simply read and write. But the next step appears exponential. I haven never done anything in VB, so any and all concepts and commands are completely foreign. I have been digging...
34
7110
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. Yensao
4
3340
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 doesn't do the way as I wished to open in Write mode. Instead, still saw (Read Only) above the MenuBar on the upper left corner when an excel file is opened.
6
12506
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# application. It seems simple enough, but the problem I'm encountering is as follows: In order for the user to select the cell from Excel, they must first click once on the Excel window to give it focus and then their second click is what changes the cell...
0
1785
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 configuration I was able to run Excel from the debugger, my add-in loads on startup in Excel, and I could debug my add in. I got a new machine. This machine has both .NET 1.14322 and .NET 2.0 installed on it and it has MS Office 11 installed. It also...
1
1261
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 it to an excel file. Mrozu was kind enough to send me some code (thank you) - unfortunately it does not work - it gives me an error "Cannot create ActiveX component." which he says is due to not having Excel installed on my machine. I DO have...
0
5573
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
2887
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 calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in case of Help, I see the Help of Excel and help of my application, both as a submenu of help. ...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.