Cannot open another Excel file while C# is working on Excel objects

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Note: I posted this same Question yesterday on Excel forum, but did not get any response. Due to the urgency I am reposting this here again

I developed an application which open the Excel and does all kind of formatting on the files in folders. Number of files can be differ from one to hundred and processing time for each file takes vary from one minute to 10 minutes. Everything working according
to the application spec, but giving another big problem. Users cant open any other Excel file while they run this application
While my application runs (opens the Excel file (visible=false) and does formatting one by one) if the user opens another Excel file, the application throwing COM related exception error and terminating. How to solve this problem?
This is how I am opening the Excel and working on that
String strFileNameWithPath = strFolderfiles.Substring(0, strFolderfiles.IndexOf( "));<br/>
String strFileName = strFolderfiles.Substring(strFolderfiles.IndexOf( ") + 1);
Microsoft.Office.Interop.Excel.Application xlApp;<br/>
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;<br/>
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;<br/>
Object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();<br/>
xlWorkBook = xlApp.Workbooks.Open(strFileNameWithPath, misValue, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,<br/>
misValue, misValue, misValue, misValue, misValue,<br/>
misValue, misValue, misValue, misValue, misValue, misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int NumberOfColumns = xlWorkSheet.Cells.Find("*", Type.Missing, Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,<br/>
Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns,<br/>
Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
false, false, Type.Missing).Column;
int numberofRows = xlWorkSheet.Cells.Find("*", Type.Missing, Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,<br/>
Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,<br/>
Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, false, false,
Type.Missing).Row;
// Adding two Columns (Station Id and Period)<br/>
((Range)xlWorkSheet.Columns[1]).Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);<br/>
((Range)xlWorkSheet.Columns[2]).Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
<br/>
//Filling the Extra colums with StationID and Period
String strPeriod = PeriodName(Period);
xlWorkSheet.Cells[1, 1] = "STATION ID";<br/>
xlWorkSheet.Cells[1, 2] = "QUARTER";<br/>
xlWorkSheet.Cells.Range[xlWorkSheet.Cells[2, 1], xlWorkSheet.Cells[numberofRows, 1]] = "Station";<br/>
xlWorkSheet.Cells.Range[xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[numberofRows, 2]] = "Period";<br/>
//xlWorkBook.Save();
//7. Date formating in Date Column (Column number 3)<br/>
xlWorkSheet.Columns["C:C", Type.Missing].NumberFormat = "mm/dd/yy";
AND this how I am closing the application and work book
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
xlWorkBook.Close();
xlApp.Quit();
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small


<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small clsCommon<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small .releaseObject(xlApp);
//releaseObject is a method to clean the garbage
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small clsCommon<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small .releaseObject(xlWorkBook);<span style="font-family:Consolas; font-size:x-small




<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small clsCommon<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small .releaseObject(xlWorkSheet);
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small ---
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small How to solve this problem? How can I allow
user to work on other Excel files while it is processing another file?
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Please help me
<hr class="sig Dreaming a world without any war in anywhere

View the full article
 
Back
Top