public Form1()
{
InitializeComponent();
InitializeExcelObjectModel();
}
~Form1()
{
DisposeExcelObjects();
}
void SaveExcel()
{
_objAppln.DisplayAlerts = false;//Since, we are using SaveFileDialog's overwrite prompt(control is on view).
_objWorkBook.SaveAs("C:\\tmp.xls",
XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_objWorkBook.Close(true, "C:\\tmp.xls", false);
_objAppln.DisplayAlerts = true;//restore back for other display alerts
}
private void InitializeExcelObjectModel()
{
_objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file
//_objAppln.Visible = true;
if (_objAppln != null)
{
_objWorkBooks = _objAppln.Workbooks;
_objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file
_objWorkSheet = (Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file
Step 1: Add the above code to a solution and add reference for Microsoft.Office.Interop.Excel.dll
Step 2: Run the application and Execute the code in button1_Click.
Step 3: While the processing is on, open any other excel file and just click on cells here and there.
COM exception occurs. Anything I am doing wrong above?
How to resolve?
June 28th, 2010, 10:55 AM
bderagon
Re: Excel crashes
Try using the following code, it looks like the interop/automation server is busy when you're trying to hit it because the dll's it needs are already in use.
I made a wrapper function that basically takes any other function (in this case, your unsafe COM calls) as a delegate, and returns an object that you can then cast back to the type you need.
The wrapper function checks to see if it gets a COM exception, and if it does, waits a slice, and tries again; you'll probably want to limit that to certain errors, or a certain number of retries, and have it throw the real error after 50 tries or something like that.
Code:
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace ExcelExport
{
public partial class Form1 : Form
{
Microsoft.Office.Interop.Excel.Application _objAppln;
Workbook _objWorkBook;
Workbooks _objWorkBooks;
Worksheet _objWorkSheet;
public Form1()
{
InitializeComponent();
InitializeExcelObjectModel();
}
void SaveExcel()
{
_objAppln.DisplayAlerts = false;//Since, we are using SaveFileDialog's overwrite prompt(control is on view).
_objWorkBook.SaveAs("C:\\tmp.xls",
XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_objWorkBook.Close(true, "C:\\tmp.xls", false);
_objAppln.DisplayAlerts = true;//restore back for other display alerts
}
private void InitializeExcelObjectModel()
{
_objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file
//_objAppln.Visible = true;
if (_objAppln == null) return;
_objWorkBooks = _objWorkBooks = _objAppln.Workbooks;
_objWorkBook = _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file
_objWorkSheet = (Worksheet) _objAppln.ActiveSheet; // To get the current active sheet in excel file
}