By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,377 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Excel Automation - code question

P: n/a

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
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mansi" schrieb
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 snipped>
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
Nov 16 '05 #2

P: n/a
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" wrote:
"Mansi" schrieb
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 snipped>
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

Nov 16 '05 #3

P: n/a
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" wrote:
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" wrote:
"Mansi" schrieb
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 snipped>
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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.