Memory can't be read when closing excel

A

Aaron Prohaska

Can anyone tell me what might be causing the following error when using
Automation through a C# windows app. I get this error after the code
below has executed and I then try and save/close excel. The odd thing
I've found is that if I close excel first I get the error below. If I
close my app and then close excel I don't get the error and the excel
instance closes properly. The problem is that I can't make users close
my app first and then save/close their excel file.

Except for this problem everything is working great.

CODE:
---------------------------------------------------------------
GC.Collect();

// Create Excel application.
Excel.Application excel;

if ( this.currentCustomer.Fields.State != EntityState.Fetched )
return;

try
{
// Get a reference to Excel.Application from ROT (Running Object Table).
excel =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch(Exception x)
{
MessageBox.Show(this, "Excel cannot be found! " + x.Message);
return;
}

Excel._Worksheet workSheet = (Excel._Worksheet)excel.ActiveSheet;

// code for editing excel goes here.

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);

excel = null;

GC.Collect();
---------------------------------------------------------------

EROROR:
---------------------------------------------------------------
The instruction at "0x300341b6" referenced memory at "0x650eeed8". The
memeory could not be "read".
---------------------------------------------------------------

Regards,

Aaron Prohaska

-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
Wrench Science Inc.
http://www.wrenchScience.com/
Phone: 510.841.4748 x206
Fax: 510.841.4708
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
 

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