Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Closing Excel from C#

0 views
Skip to first unread message

Jason Hagar

unread,
May 31, 2002, 5:30:08 PM5/31/02
to
Hi

I have a Windows Forms application that I am using to automate some tasks within
Excel (cannot be done within Excel, already looked at this). I also have a
separate project that sort-of wraps around Excel and makes operations simpler to
the Windows Forms application. The class in the separate project that wraps
Excel (call it ExcelHelper) seemed like a perfect place to implement IDisposable
and provide a destructor, to ensure that Excel is closed. However, whenever I
try to call any methods from Dispose, I get a first chance exception of type
System.Runtime.InteropServices.COMException that states that "The application
called an interface that was marshalled for a different thread." I discovered
that my Dispose method was being called by my class' destructor (I guess I
forgot to have my WinForm clean up the ExcelHelper, because I use the
GC.SuppressFinalize method). This happens whenever I try to access the Save
method of the _Workbook class. I can access the UserControl property or Quit
method of the Excel.ApplcationClass class, but other properties of this object
(such as Columns or Cells) give the same error. It seems that VS can't get
properties from these objects either because its Autos window displays <error:
an exception of type: {System.Runtime.InteropServices.COMException} occurred>
for the value of a property.

Can anyone help me? I tried applying [STAThread] to my Main function, but that
didn't help.

Thanks,
Jason

Greg Ewing

unread,
May 31, 2002, 5:47:35 PM5/31/02
to
Jason, if you just want to close Excel see redundanman's earlier post and
Nicholas Paldino's reply with the subject "Controlling Excel from C#".

If that's not what you are trying to do I must admit I don't understand your
post. Could you clarify? Some sample code would be great.

--
Greg
http://www.claritycon.com/

"Jason Hagar" <hag...@agcs.com> wrote in message
news:3CF7EB60...@agcs.com...

Jason Hagar

unread,
May 31, 2002, 6:10:44 PM5/31/02
to
Greg,
This is a simple example of what I am doing. I read Nicholas' example, and
did what he said, but the difference, is that I am working with Excel from a
different object than the Windows Forms application. I've tried Disposing of my
helper object, but I still get the exception.

Thanks,
Jason


-- Project: WindowsFormsApplication1 --
class MainForm : Form
{
ExcelHelper helper;

public MainForm()
{
helper = new ExcelHelper("fileToOpen.xls");
Button button = new Button;
button.Text = "Exit";
button.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(button);
}
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
helper.Dispose();
Application.Exit();
}
}

-- Project ExcelHelper --
class ExcelHelper : IDisposable
{
private Excel.Application xl = null;
Excel._Workbook book;

public ExcelHelper(string xlsFileToOpen)
{
// do stuff to open excel and workbook
}
public void Dispose()
{
try
{
book.Save();
xl.Quit();
} catch { // I always get here because book.Save() causes COMException
} finally {
Marshal.ReleaseComObject(xl);
xl = null;
GC.Collect();
GC.SuppressFinalize();
}
}
~ExcelHelper()
{
this.Dispose();

0 new messages