A simple code to export data to excel

public partial class Form1 : Form

Microsoft.Office.Interop.Excel.Application _objAppln;
Workbook _objWorkBook;
Workbooks _objWorkBooks;
Worksheet _objWorkSheet;

public Form1()


void SaveExcel()

_objAppln.DisplayAlerts = false;//Since, we are using SaveFileDialog's overwrite prompt(control is on view).

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


public void DisposeExcelObjects()

_objWorkSheet = null; _objWorkBooks = null; _objWorkBooks = null; _objAppln = null;

private void button1_Click(object sender, EventArgs e)

Range objRange=null;
string cell1 = string.Empty, cell2 = string.Empty;
string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T" };

for (int row = 1; row &lt; 1000; row++)
for (int column = 0; column &lt; 20; column++)
cell1 = chars[column] + row.ToString();
objRange = _objWorkSheet.get_Range(cell1, cell1);
objRange.Value2 = cell1;

objRange = null;

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?