'System.InvalidCastException'

  • Thread starter Thread starter Julien
  • Start date Start date
J

Julien

Hi !!!

I hav a problem when I try to initialize my workbook (opening or creation)
in my windows form...
I got the Exception

'System.InvalidCastException' : QueryInterface for interface
Excel._Application failed

I tried to change de CultureInfo but it doesn't work better.
I am working with VS.Net and Excel 2000

is there something else to do ?

thanks

Julien
 
Julien,

Is it possible that you are running this on a machine that has a
different version of Excel than the product was developed for? If so, then
you will have to resort to either late binding, or performing a conditional
switch, and have references to each version's interfaces.

Hope this helps.
 
I am trying to develop with excel 2000, maybe it will be better that I try
Excel XP !!!


Nicholas Paldino said:
Julien,

Is it possible that you are running this on a machine that has a
different version of Excel than the product was developed for? If so, then
you will have to resort to either late binding, or performing a conditional
switch, and have references to each version's interfaces.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Julien said:
Hi !!!

I hav a problem when I try to initialize my workbook (opening or creation)
in my windows form...
I got the Exception

'System.InvalidCastException' : QueryInterface for interface
Excel._Application failed

I tried to change de CultureInfo but it doesn't work better.
I am working with VS.Net and Excel 2000

is there something else to do ?

thanks

Julien
 
It might help if you would post some code of the problem.
My guess is that what you are trying to send to excel isn't something
excel can handle.
Not sure what changing cultureinfo would fix. Then again, some code would
be helpful.
 
Julien,

Where did you get the primary interop assemblies that you are using in
..NET to access Excel from?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Julien said:
I am trying to develop with excel 2000, maybe it will be better that I try
Excel XP !!!


Nicholas Paldino said:
Julien,

Is it possible that you are running this on a machine that has a
different version of Excel than the product was developed for? If so, then
you will have to resort to either late binding, or performing a conditional
switch, and have references to each version's interfaces.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Julien said:
Hi !!!

I hav a problem when I try to initialize my workbook (opening or creation)
in my windows form...
I got the Exception

'System.InvalidCastException' : QueryInterface for interface
Excel._Application failed

I tried to change de CultureInfo but it doesn't work better.
I am working with VS.Net and Excel 2000

is there something else to do ?

thanks

Julien
 
Thanks for replying, here is the code :

=======================================================
private Excel.Application ExcelObj = null;

public Form1()
{
// Initialize the Windows Components
InitializeComponent();
ExcelObj = new Excel.Application();
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
// *** open the workbook --> it goes wrong on this line
Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open("c:\counter.xls",Type.Missing, Type.Missing,
Type.Missing,Type.Missing,Type.Missing, Type.Missing,
Excel.XlPlatform.xlWindows, Type.Missing,Type.Missing,
Type.Missing,Type.Missing, Type.Missing);
// *** get the collection of sheets in the workbook
Excel.Sheets sheets = theWorkbook.Worksheets;
// *** get the first and only worksheet from the collection of worksheets
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
....
}
===============================================================

concerning the primary interop assemblies, under VS.Net I choose project>add
a reference>COM and then I select "Microsoft Excel 9 Object Librairy",
filename is "excel9.olb" ; it puts the interop.excel.dll and
interop.office.dll in my project folder... I have problem to understand the
what is Interop...

Thanks for your help


_____________________________________________________________________
"Morten Wennevik" <[email protected]> a écrit dans le message de
oprxs3y0dbhntkfz@localhost...
It might help if you would post some code of the problem.
My guess is that what you are trying to send to excel isn't something
excel can handle.
Not sure what changing cultureinfo would fix. Then again, some code would
be helpful.
 
// *** open the workbook --> it goes wrong on this line
Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open(
"c:\counter.xls",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlPlatform.xlWindows,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);

Something among these paramteres is of the wrong type, but I couldn't find
much reference to WoorkBooks.Open in my helpfiles or MSDN, so I'm not sure
what it expects.

http://www.dotnet247.com/247reference/msgs/22/113113.aspx
used this method to open a woorkbook

Excel.Workbook Workbook = objExcel.Workbooks.Open(
ImportFileName,
0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
false);
 
I already try all possibilities but nothing works... I don't know if this is
the code but maybe it comes from the interops ??


Morten Wennevik said:
// *** open the workbook --> it goes wrong on this line
Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open(
"c:\counter.xls",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlPlatform.xlWindows,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);

Something among these paramteres is of the wrong type, but I couldn't find
much reference to WoorkBooks.Open in my helpfiles or MSDN, so I'm not sure
what it expects.

http://www.dotnet247.com/247reference/msgs/22/113113.aspx
used this method to open a woorkbook

Excel.Workbook Workbook = objExcel.Workbooks.Open(
ImportFileName,
0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
false);
 
Back
Top