B
Bob Dankert
I have searched the newsgroups and found numerous posts on this issue, and I
am joining the scores of people who can not get Excel to end while using it
through Com. I think I have tried everything that has been suggested all
over the place, and I have had no luck, there is always an Excel process
running after I finish this.
In the code, I know that the code within the catch's are not correct, but it
never makes it to any of the catches as a messagebox is never displayed, and
I have stepped through the code numerous times line by line.
I would love any help or suggestions anyone may have, as this is really
driving me crazy. Thanks a lot for any help,
Bob Dankert
The code is here:
private bool getDataFromExcelFile(string fileName)
{
Excel.Application app;
Excel.Workbook book;
Excel.Worksheet sheet;
Excel.Workbooks books;
Excel.Range cells;
object oMissing = System.Reflection.Missing.Value;
object False = (object) false;
object True = (object) true;
Array exlSheet;
partNumber = new ArrayList();
try
{
app = new Excel.ApplicationClass();
try
{
books = app.Workbooks;
book = books.Open(fileName, False, True, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing);
}
catch
{
sheet = null;
book = null;
app = null;
MessageBox.Show("This is not a valid Excel file. Please select a
different file for processing.", "File Error", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
this.valid = false;
return false;
}
sheet = (Excel.Worksheet) book.ActiveSheet;
//column 12 is code, 1 is number, rows 4 - end
//we are looking for planner codes 15, 1, 4
Excel.Range rng = sheet.UsedRange;
cells = rng.Cells;
exlSheet = (Array) cells.Value2;
for(int a =4; a < exlSheet.GetLength(0) + 1; a++)
{
try
{
if(int.Parse(exlSheet.GetValue(a, 12).ToString()) == 1 ||
int.Parse(exlSheet.GetValue(a, 12).ToString()) == 4 ||
int.Parse(exlSheet.GetValue(a, 12).ToString()) == 15)
{
partNumber.Add((object)exlSheet.GetValue(a,1).ToString());
}
}
catch
{
MessageBox.Show("This is not a valid Bill of Materials. Please select
a new excel file.", "File Error", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
this.valid = false;
return false;
}
}
book.Close(oMissing, oMissing, oMissing);
books.Close();
app.Quit();
NAR(sheet);
NAR(rng);
NAR(cells);
NAR(books);
NAR(book);
NAR(app);
oMissing = null;
GC.Collect();
GC.WaitForPendingFinalizers();
int aa=0;
}
catch(Exception e)
{
MessageBox.Show("There has been an error reading from the Excel file.
The specific error was:\n\n" + e.ToString());
this.valid = false;
return false;
}
return true;
}
private void NAR(object o)
{
try
{
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) != 0)
{}
}
catch {}
finally
{
o = null;
}
}
am joining the scores of people who can not get Excel to end while using it
through Com. I think I have tried everything that has been suggested all
over the place, and I have had no luck, there is always an Excel process
running after I finish this.
In the code, I know that the code within the catch's are not correct, but it
never makes it to any of the catches as a messagebox is never displayed, and
I have stepped through the code numerous times line by line.
I would love any help or suggestions anyone may have, as this is really
driving me crazy. Thanks a lot for any help,
Bob Dankert
The code is here:
private bool getDataFromExcelFile(string fileName)
{
Excel.Application app;
Excel.Workbook book;
Excel.Worksheet sheet;
Excel.Workbooks books;
Excel.Range cells;
object oMissing = System.Reflection.Missing.Value;
object False = (object) false;
object True = (object) true;
Array exlSheet;
partNumber = new ArrayList();
try
{
app = new Excel.ApplicationClass();
try
{
books = app.Workbooks;
book = books.Open(fileName, False, True, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing);
}
catch
{
sheet = null;
book = null;
app = null;
MessageBox.Show("This is not a valid Excel file. Please select a
different file for processing.", "File Error", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
this.valid = false;
return false;
}
sheet = (Excel.Worksheet) book.ActiveSheet;
//column 12 is code, 1 is number, rows 4 - end
//we are looking for planner codes 15, 1, 4
Excel.Range rng = sheet.UsedRange;
cells = rng.Cells;
exlSheet = (Array) cells.Value2;
for(int a =4; a < exlSheet.GetLength(0) + 1; a++)
{
try
{
if(int.Parse(exlSheet.GetValue(a, 12).ToString()) == 1 ||
int.Parse(exlSheet.GetValue(a, 12).ToString()) == 4 ||
int.Parse(exlSheet.GetValue(a, 12).ToString()) == 15)
{
partNumber.Add((object)exlSheet.GetValue(a,1).ToString());
}
}
catch
{
MessageBox.Show("This is not a valid Bill of Materials. Please select
a new excel file.", "File Error", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
this.valid = false;
return false;
}
}
book.Close(oMissing, oMissing, oMissing);
books.Close();
app.Quit();
NAR(sheet);
NAR(rng);
NAR(cells);
NAR(books);
NAR(book);
NAR(app);
oMissing = null;
GC.Collect();
GC.WaitForPendingFinalizers();
int aa=0;
}
catch(Exception e)
{
MessageBox.Show("There has been an error reading from the Excel file.
The specific error was:\n\n" + e.ToString());
this.valid = false;
return false;
}
return true;
}
private void NAR(object o)
{
try
{
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) != 0)
{}
}
catch {}
finally
{
o = null;
}
}