Excel Automation - code question

G

Guest

I'm trying to use excel automation (from visual c# .NET) to fill data in a
range by using arrays. I am using Microsoft's Knowledge Base Article 302096
as a reference and using the example listed there, but I'm having trouble
with the following code:

**************** Code Starts Here ***********
Excel.Application m_objExcel;
Excel.Workbooks m_objBooks;
Excel._Workbook m_objBook;
Excel.Sheets m_objSheets;
Excel._Worksheet m_objSheet;
Excel.Range m_objRange;

m_objExcel = new Excel.Application();
m_objExcel.Visible = true;

m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)m_objBooks.Add(Missing.Value);

m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));

m_objRange = m_objSheet.get_Range("A1", Missing.Value);
m_objRange = m_objRange.get_Resize(5, 5);

double[,] doubleGrid = new double[5,5];

for (int row = 0; row < 5; row++)
{
for (int column = 0; column < 5; column++)
{
doubleGrid[row,column] = row * column;
}
}

m_objRange.set_Value(Missing.Value, doubleGrid);

*************** Code Ends Here *************

Microsoft Excel opens up okay, but then the server throws an exception (in
excel). I'm not able to figure out what the problem is, but I have a feeling
that it's something with Range.set_Value method.

Any ideas??

Thanks.

Mansi
 
A

Arne Janning

I'm trying to use excel automation (from visual c# .NET) to fill data in a
range by using arrays. I am using Microsoft's Knowledge Base Article
302096
as a reference and using the example listed there, but I'm having trouble
with the following code:

Microsoft Excel opens up okay, but then the server throws an exception (in
excel). I'm not able to figure out what the problem is, but I have a
feeling
that it's something with Range.set_Value method.

Hi Mansi,

I just copied your code into a new project and it worked without problems on
my machine
(Excel 2003, PIAs installed, .NET Framework 1.1 [no SP1], WinXP SP2).

Can you give us more details about the exception?

Which version of Excel are you using? PIAs installed?

Cheers

Arne Janning
 
G

Guest

Here are the details of what I have installed:
(Excel 2003, PIAs have been installed, .NET Framework 1.1 [not sure about
SP1], WinXP SP2)

Here are more details about the exception (contents of Exception object):
(1) Exception.Message = "The server threw an exception"
(2) Exception.Source = "mscorlib"

This exception is generated after running the Range.set_Value method.

What other Exception properties would be helpful for you to determine the
cause of the error?

Thanks for trying this out.

Mansi


Arne Janning said:
I'm trying to use excel automation (from visual c# .NET) to fill data in a
range by using arrays. I am using Microsoft's Knowledge Base Article
302096
as a reference and using the example listed there, but I'm having trouble
with the following code:

Microsoft Excel opens up okay, but then the server throws an exception (in
excel). I'm not able to figure out what the problem is, but I have a
feeling
that it's something with Range.set_Value method.

Hi Mansi,

I just copied your code into a new project and it worked without problems on
my machine
(Excel 2003, PIAs installed, .NET Framework 1.1 [no SP1], WinXP SP2).

Can you give us more details about the exception?

Which version of Excel are you using? PIAs installed?

Cheers

Arne Janning
 
G

Guest

Hi Arne,

I just tried modifying the following declaration:
double[,] doubleGrid = new double[5,5]; to
object[,] doubleGrid = new object[5,5];

This seemed to solve my problem. Then I changed the declaration back to the
original, double[,] doubleGrid = new double[5,5]. I was expecting that I
would get the earlier error again. But it still worked okay. I'm not sure
what's going on???

What's the root cause of this funny behavior? I'm concerned that I'll run
into a similar situation again and not now how to debug it.

Thanks for all the help.

Mansi

Mansi said:
Here are the details of what I have installed:
(Excel 2003, PIAs have been installed, .NET Framework 1.1 [not sure about
SP1], WinXP SP2)

Here are more details about the exception (contents of Exception object):
(1) Exception.Message = "The server threw an exception"
(2) Exception.Source = "mscorlib"

This exception is generated after running the Range.set_Value method.

What other Exception properties would be helpful for you to determine the
cause of the error?

Thanks for trying this out.

Mansi


Arne Janning said:
I'm trying to use excel automation (from visual c# .NET) to fill data in a
range by using arrays. I am using Microsoft's Knowledge Base Article
302096
as a reference and using the example listed there, but I'm having trouble
with the following code:

Microsoft Excel opens up okay, but then the server throws an exception (in
excel). I'm not able to figure out what the problem is, but I have a
feeling
that it's something with Range.set_Value method.

Hi Mansi,

I just copied your code into a new project and it worked without problems on
my machine
(Excel 2003, PIAs installed, .NET Framework 1.1 [no SP1], WinXP SP2).

Can you give us more details about the exception?

Which version of Excel are you using? PIAs installed?

Cheers

Arne Janning
 

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