Excel WorkBook.OpenText returns VOID!!!

C

captain hermdog

Hi all,

As you may have guess, I'm working with the Excel object in C#. I am
trying to open a comma-delimited (.csv) file in Excel so I can easily
transfer the data to another Excel WorkBook.

I have read many examples online which all reference the
WorkBook.OpenText method -- for example:

myWorkBook = myApplication.WorkBooks.OpenText(blah, blah, ...);

Here is the problem, When I try to implement that code the OpenText
method returns void - thus rendering a compiler error.

COM Library
-----------
Microsoft Excel 11.0 Object Library

Code
----
rdWorkBook = rdApp.Workbooks.OpenText(
rawDataFile,
Type.Missing,
1,
Excel.XlTextParsingType.xlDelimited,
Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
Type.Missing,
Type.Missing,
Type.Missing,
true,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);

---------------------
So, in order to work past this issue I used the WorkBook.Open method.
I can open the .csv file ok, however it is not recognizing the comma
delimited notation - so it essentially opens the file as a text file
rather than an Excel file (if you know what I mean).

Here is my code:

myWorkBook = myApp.Workbooks.Open(
rawDataFile,
0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
",",
false,
false,
0,
true,
false,
false);
---------------------

Anyhow, does anyone know what's up? Is there a better way?

Thanks in advance!
Jes
 
C

captain hermdog

Ok I found the solution. You need to open the file via the
application, rather than the workbook.

myApp = new Excel.ApplicationClass();

// open the file using the Application object
myApp.WorkBooks.OpenText(blah, blah, ...);

// then you need to associate the workbook with the application
myWorkBook = myApp.ActiveWorkBook;

There it is!
 

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