Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Process Won't End 1

Status
Not open for further replies.

Goodall

MIS
Joined
Aug 4, 2004
Messages
18
Location
US
I cannot seem to end a Microsoft Excel process. I create the Application, Workbook and Worksheet in that order. I load the worksheet data and save the workbook. I make the application visibile and pass usercontrol to it, then close (set to nothing) the Worksheet, Workbook and Application in that order. I figured that would "free up" VB of the objects. When I close the Excel application, the Excel.exe process does not end. The Task Manager shows a seperate process still running for each time I run the VB app.

Why doesn't the Excel process end?

My code is below.


Sub SaveAsExcel()
Dim FileName As String = cboFileName.Text
Dim FileExtension As String = ".xls"
xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
LoadWorksheet()
xlBook.SaveAs("C:\" & FileName & FileExtension)
xlApp.Visible = True
xlApp.UserControl = True
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
'xlApp.Quit()
End Sub
 
Also do a Marshal.ReleaseComObject(EXCEL_OBJECT) on your objects.
 
xlApp.Quit() will shut down Excel and release the objects, but the process still runs. I don't want the application to shut down Excel. I want the user to have control of that from within Excel.

I Released the COM objects as follows.
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

The Excel process still runs.

How do I reference the three individual worksheets that VB creates on my behalf when I created the workbook?
 
>xlApp.Quit() will shut down Excel and release the objects, but the process still runs.

Weirdly enough, I think Excel will remain up -- after xlApp.Quit() -- until the application itself is closed. At least, that's what I have observed before.
 
I think the problem has to do with LoadWorksheet(). Perhaps try placing the code in that method into SaveAsExcel(), and make sure you destroy any variables that reference Excel. I usually use system.gc.collect() at the end of any method that uses Excel, just as extra pre-caution.

Hope that helps.
 
Everyone, thanks for the multiple suggestions. The star goes to miket26. I added "System.GC.Collect()" to my code after all the objects had been set to nothing. Now the Excel.exe process stops running when Excel is shut down, just as I wanted. And it doesn't matter if the VB application is running or not.

Sub SaveAsExcel()
Dim FileName As String = cboFileName.Text
Dim FileExtension As String = ".xls"
xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
LoadWorksheet()
xlBook.SaveAs("C:\" & FileName & FileExtension)
xlApp.Visible = True
xlApp.UserControl = True
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
System.GC.Collect()
End Sub

A final off-shoot question regarding these posts. How does one encapsulate code in "box" like ThatRickGuy did?
 
Type:
[ignore]
Code:
 line of code
 lineof code
[/ignore]
 
Click on Process TGML, at the bottom of the post window (right next to the Emoticons/Smileys), for more details.
 
I always thought you had to be special to that.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top