Cannot get Excel process to end

  • Thread starter Thread starter Bob Dankert
  • Start date Start date
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;
}
}
 
why should it make it thru the catch block? That only happens when an
exception is thrown.
You also have a while loop that can potentially go on forever. It's also
extremely expensive to wait on an object with a while loop. Maybe you can
explain what exactly is failing.
 
Hi

Also you may try to take a look at the link below.
http://groups.google.com/groups?hl=zh-CN&lr=&c2coff=1&threadm=0RPKrldcDHA.20
00%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fhl%3Dzh-CN%26lr%3D%26c2coff%
3D1%26q%3Dexcel%2Bapplication%2B%2522peter%2Bhuang%2522%2Bmarshal

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Peter,

We will try switching from early binding to late binding and see if this
makes a difference in our situation. I will let you know in the next couple
days.

Thanks for the help,

Bob Dankert
 
Alvin, maybe I was not being clear. I was simply stating that I realize
that if an exception were indeed thrown, that an Excel process would remain
running because my code in the catch block will not end the Excel process.
I was also trying to state that I know for certain that there are no
exceptions being thrown, and this is not the source of my problem.

The problem is that after that code executes, no exceptions are thrown, and
there consistently remains one process of Excel running after everything is
called.

I have yet to try Peter's suggestion with late binding as opposed to
early-binding as I am currently using, I will report back with the status of
that.

Bob Dankert

Alvin Bruney said:
why should it make it thru the catch block? That only happens when an
exception is thrown.
You also have a while loop that can potentially go on forever. It's also
extremely expensive to wait on an object with a while loop. Maybe you can
explain what exactly is failing.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Bob Dankert said:
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;
}
}
 
ok,
let us know

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Bob Dankert said:
Alvin, maybe I was not being clear. I was simply stating that I realize
that if an exception were indeed thrown, that an Excel process would
remain running because my code in the catch block will not end the Excel
process. I was also trying to state that I know for certain that there are
no exceptions being thrown, and this is not the source of my problem.

The problem is that after that code executes, no exceptions are thrown,
and there consistently remains one process of Excel running after
everything is called.

I have yet to try Peter's suggestion with late binding as opposed to
early-binding as I am currently using, I will report back with the status
of that.

Bob Dankert

Alvin Bruney said:
why should it make it thru the catch block? That only happens when an
exception is thrown.
You also have a while loop that can potentially go on forever. It's also
extremely expensive to wait on an object with a while loop. Maybe you can
explain what exactly is failing.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Bob Dankert said:
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;
}
}
 
Hi

Look forward to hearing from you.:)

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi

How is the things going, if you have update for this issue, please feel
free to post here?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top