Excel instances not closing

G

Guest

Hello,

I have a VB.NET application that needs to open an Excel workbook. It runs
fine and when I have the Visible property set to true, everything appears to
work as it is supposed to (Excel opens, the workbook opens, processing works,
and the workbook and Excel close) but when I go to Task Manager the instance
of Excel is still running. If I run the program repeated times, there is an
instance of Excel running for each time I ran the VB application. Here is
the code I'm using to open and close Excel:

***************************************************

' class members
Dim excelApp As Excel.Application
Dim excelWb As Excel.Workbook

Private Sub openExcel()
Try
excelApp = New Excel.Application
excelApp.Visible = True
Catch ex As Exception
MessageBox.Show("Excel Open Exception:" + ex.Message, "Excel
Exception", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub closeExcel()
Try
excelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)

excelWb = Nothing
excelApp = Nothing
Catch ex As Exception
MessageBox.Show("Excel Close Exception:" + ex.Message, "Excel
Exception", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

***************************************************

Any help would be greatly appreciated.

Thanks in advance,
Rob
 
J

Jim Cone

Rob,
Maybe ...
http://support.microsoft.com/?­kbid=317109
"When you automate a Microsoft Office application from
Microsoft Visual Basic .NET or Microsoft Visual C# .NET,
the Office application does not quit when you call the Quit method"

Regards,
Jim Cone
San Francisco, USA


Rob Kellow said:
Hello,

I have a VB.NET application that needs to open an Excel workbook. It runs
fine and when I have the Visible property set to true, everything appears to
work as it is supposed to (Excel opens, the workbook opens, processing works,
and the workbook and Excel close) but when I go to Task Manager the instance
of Excel is still running. If I run the program repeated times, there is an
instance of Excel running for each time I ran the VB application. Here is
the code I'm using to open and close Excel:

-snip-
 
F

Fredrik Wahlgren

Rob Kellow said:
Hello,

I have a VB.NET application that needs to open an Excel workbook. It runs
fine and when I have the Visible property set to true, everything appears to
work as it is supposed to (Excel opens, the workbook opens, processing works,
and the workbook and Excel close) but when I go to Task Manager the instance
of Excel is still running. If I run the program repeated times, there is an
instance of Excel running for each time I ran the VB application. Here is
the code I'm using to open and close Excel:

***************************************************

' class members
Dim excelApp As Excel.Application
Dim excelWb As Excel.Workbook

Private Sub openExcel()
Try
excelApp = New Excel.Application
excelApp.Visible = True
Catch ex As Exception
MessageBox.Show("Excel Open Exception:" + ex.Message, "Excel
Exception", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub closeExcel()
Try
excelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)

excelWb = Nothing
excelApp = Nothing
Catch ex As Exception
MessageBox.Show("Excel Close Exception:" + ex.Message, "Excel
Exception", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

***************************************************

Any help would be greatly appreciated.

Thanks in advance,
Rob

Do you save the workbook before you try to close Excel? What happens if you
just create an instance of Excel, set the visiblility to true and then close
Excel?

/Fredrik
 
G

Guest

Hi Fredrik,

That's very interesting. If I open the workbook but don't save it, it still
leaves the instance of Excel running (I see it in Task Manager) but if I
don't open a workbook and then just close Excel, everything works and the
instance of Excel is gone from Task Manager. Unfortunately, not opening a
workbook isn't an option for me, in fact it makes Excel kind of useless if
you can't open a workbook.

Do you have any other suggestions?

Thanks and Regards,
Rob
 
F

Fredrik Wahlgren

Rob Kellow said:
Hi Fredrik,

That's very interesting. If I open the workbook but don't save it, it still
leaves the instance of Excel running (I see it in Task Manager) but if I
don't open a workbook and then just close Excel, everything works and the
instance of Excel is gone from Task Manager. Unfortunately, not opening a
workbook isn't an option for me, in fact it makes Excel kind of useless if
you can't open a workbook.

Do you have any other suggestions?

Thanks and Regards,
Rob

Yes, I have more suggestions. I think this shows that there is something
with the workbook that prevents excel from closing properly.

Are you saying that Excel will stay open if you just open the workbook and
don't do anything with it. I guess this could happen if excel thinks the
workbook needs to be saved. This can happen if you have volatile functions
within the workbook. I think that the the workbook object has a property
called saved or similar. After you open the workbook, set this property to
true. Then close Excel.

Best Regards,
Fredrik
 
G

Guest

Hi Fredrik,

Thanks for getting back to me I appreciate it. OK, so here's what happens.
You're correct in what you asked below, I only have to open the workbook for
the problem to occur, no changes or anything else. I set Visible to true so
I could watch it and I don't see anything wrong when the workbook opens and
closes but still the Excel instance remains. I then tried what you
suggested, I first save the workbook before I try to close it and I then
changed my Close parameter to true (from false) which tells Excel to save
before closing and neither thing seemed to make a difference. The Excel
instance remains in Task Manager. One other thing that is a little
interesting, if I don't try to close Excel with my application (I commented
out the call to close Excel) after the application is done I have Excel
sitting there and if I close it myself, the instance of Excel in Task Manager
goes away, even if it opened the workbook. So, it's not how I'm opening it
or the workbook necessarily but the closing of Excel itself that seems to be
the problem.

I noticed several other threads on this exact issue and it concerns me that
it appears that none of them have ever been resolved. By the way, from the
other threads, it appears that it is isolated to Excel 2003, which is what
I'm running.

Thanks again for your help and if you can think of anything else for me to
try please let me know.

Regards,
Rob
 
F

Fredrik Wahlgren

Rob Kellow said:
Hi Fredrik,

Thanks for getting back to me I appreciate it. OK, so here's what happens.
You're correct in what you asked below, I only have to open the workbook for
the problem to occur, no changes or anything else. I set Visible to true so
I could watch it and I don't see anything wrong when the workbook opens and
closes but still the Excel instance remains. I then tried what you
suggested, I first save the workbook before I try to close it and I then
changed my Close parameter to true (from false) which tells Excel to save
before closing and neither thing seemed to make a difference. The Excel
instance remains in Task Manager. One other thing that is a little
interesting, if I don't try to close Excel with my application (I commented
out the call to close Excel) after the application is done I have Excel
sitting there and if I close it myself, the instance of Excel in Task Manager
goes away, even if it opened the workbook. So, it's not how I'm opening it
or the workbook necessarily but the closing of Excel itself that seems to be
the problem.

I noticed several other threads on this exact issue and it concerns me that
it appears that none of them have ever been resolved. By the way, from the
other threads, it appears that it is isolated to Excel 2003, which is what
I'm running.

Thanks again for your help and if you can think of anything else for me to
try please let me know.

Regards,
Rob

Hmm.. It could be the calculation setting that does this. I have found that
the Calulation setting under Tools|Settings is a document setting, not an
excel setting. Most people don't realize this. If you open the
Tools|Settings window and move to the Calculation tab you will see that
there are two radio buttons and one checkbox. These settings allow you to
chose between Automatic or Manual calculation mode. If you chose manual
mode, you can then also select the "Calculate and save" option. Ther's
actually a third radio butoon, automatic except tables, but I don't think
that's relevant for this problem

So, I want you to open the sheet and then see what the settings are for this
document. Most likely, the calculation is Automaticc. Change to manual mode
and selct the "Calculate and save" option.

If this doesn't help, I want you to create a new blank document which you
save and let your code open instead of the one yopu open now. Try different
calculation settings.

Best Regards,
Fredrik
 
G

Guest

Hi Fredrik,

Thanks again for your help. OK, I tried everything you suggested and
nothing made a difference. You were correct in that the Calculation setting
was set to Automatic and was indeed a sheet setting not an Excel setting but
that didn't fix the problem and I tried all the possible combinations. I
also created a new sheet and tried all the calculations settings on that as
well and still I'm left with the Excel instances still in memory.

Do you have any other thoughts on what it could be? I've exhausted
everything I can think of. I really appreciate the help so if there is
anything else you can come up with I'll give it a try.

Thanks again,
Rob
 
F

Fredrik Wahlgren

Rob Kellow said:
Hi Fredrik,

Thanks again for your help. OK, I tried everything you suggested and
nothing made a difference. You were correct in that the Calculation setting
was set to Automatic and was indeed a sheet setting not an Excel setting but
that didn't fix the problem and I tried all the possible combinations. I
also created a new sheet and tried all the calculations settings on that as
well and still I'm left with the Excel instances still in memory.

Do you have any other thoughts on what it could be? I've exhausted
everything I can think of. I really appreciate the help so if there is
anything else you can come up with I'll give it a try.

Thanks again,
Rob

Not right now. I have VS2003 and Excel 2003. If you can create a minimal
project that has this problem, send it to me and I will try it.

Best regards,
Fedrik
 
F

Fredrik Wahlgren

Rob Kellow said:
Hi Fredrik,

Thanks again for your help. OK, I tried everything you suggested and
nothing made a difference. You were correct in that the Calculation setting
was set to Automatic and was indeed a sheet setting not an Excel setting but
that didn't fix the problem and I tried all the possible combinations. I
also created a new sheet and tried all the calculations settings on that as
well and still I'm left with the Excel instances still in memory.

Do you have any other thoughts on what it could be? I've exhausted
everything I can think of. I really appreciate the help so if there is
anything else you can come up with I'll give it a try.

Thanks again,
Rob

I found an article which I failed to mention. Here it is:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109

Best Regards,
Fredrik
 
G

Guest

Hi Fredrik,

I'll Zip up the VS solution and send it to you but where should I send it?
I can't seem to attach a Zip file to these responses. Also, I saw that
article too and when you get my code you'll see that I implemented the NAR
sub... didn't help.

Thanks again,
Rob
 
F

Fredrik Wahlgren

Hi

Here's my exceptionally long email address
(e-mail address removed)

/Fredrik
 
G

Guest

Hello Everyone,

Thanks to Fredrik he found and provided this link
http://www.mswordtalk.com/ftopic23462.html and the steps in this thread fixes
the problem. It turns out to be a "side-effect" of the Google Desktop search
engine (I never would have guessed). I just verified that everything works
as soon as the Google COM Add-in is turned off in Excel.

Thanks for everyone's help,
Rob
 

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