multiple instances of excel process gets created in the task manager

S

shantanu

Hello
I am sharing the code to open an excel workbook, but when i am
executing the code after MESSAGE BOX 2, multiple instances of excel
process gets created in the task manager and the excel application
hangs. kindly help

MessageBox.Show("1");
Excel.Application excelApp = new Excel.ApplicationClass
(); // Creates a new Excel Application
//excelApp.Visible = false; // Makes Excel visible to
the
user.


// The following line adds a new workbook
//Excel.Workbook newWorkbook = excelApp.Workbooks.Add
(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);


// The following code opens an existing workbook
MessageBox.Show("2");
Excel.Workbook _book;
string workbookPath = @"D:\Window Crawler\GenPatchReport
\Test Data\Data.xls"; // Add your own path here
_book = OpenExcelWorkbook(workbookPath);


//Excel.Workbook excelWorkbook = excelApp.Workbooks.Open
(workbookPath, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);


MessageBox.Show("3");


Excel.Sheets excelSheets = _book.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet =(Excel.Worksheet)
excelSheets.get_Item(currentSheet);


MessageBox.Show("4");


Excel.Range excelCell =(Excel.Range)
excelWorksheet.get_Range("A1", "A13");
excelCell.Delete(Type.Missing);
NAR(excelWorksheet);


MessageBox.Show("5");


protected Excel.Workbook OpenExcelWorkbook(string fileName)
{


try
{
if (ExcelApp != null)
{
Excel.Workbook wb;
//wb = ExcelApp.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
wb = ExcelApp.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (wb != null)
{
_sheets = wb.Worksheets;
}
return wb;


}
return null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return null;
}
}


private void NAR(object o)
{
try
{

System.Runtime.InteropServices.Marshal.ReleaseComObject
(o);
}
catch { }
finally
{
o = null;
}
}


Thanks
Shantanu
 

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