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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Excel from VB6 2

Status
Not open for further replies.

jjoy123123

Programmer
Oct 22, 2002
20
0
0
US
I'm trying to close my excel application after displaying the spreadsheet. I know that in order to release the memory, I have to:

excel.application.close
excel.application = nothing

However, since I'm displaying the Excel report, the user closes it by clicking on the X in the Excel window. After doing this, there is still an instance of Excel running in my task manager. Is there anyway to execute the above 2 lines of code when a user clicks on the X to close the Excel window?
 
jjoy..
No there really is not a way to call your VB code from Excel in the manner you desire. Excel app has the focus at that time and you are not programming directly into the Excel executable.
The code below is from a Word project, but it provides you with the basics you need to figure it out.
Put this in your main form of the VB Project, change it to excel app. and when your app. closes, Windows will remove that instance of Excel, when it has time to do it.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If objWord Is Nothing Then
Exit Sub
Else
objWord.Quit
Set objWord = Nothing
End If
End Sub
 
Once I close the form that calls Excel, I can get that instance of Excel to quit.

Here's the scenario - the form consists of several buttons that display different graphs in Excel. Once you've viewed one (and then exit Excel), I would like for to be able to view another one without closing out of that form.

Any ideas?
 
After the user has closed Excel reopen only your Excel file do not recreate your Excel.Application object variable twice(CreateObject), I remember I had some problems doing this.
Note: when user closes Excel (s)he is killing that specific Excel instance of Excel but your variable is still an Excel app object that can be reassigned.
I think that the only way I got ride of that instance of msacess in the task manager was with XLApp.Quit when closing the vb form, where XLApp is your Excel.Application created object.

Good luck!!
 
I've figured out a solution. I took the code HobbitK gave me and I execute it once a user selects a graph. That way, I will always kill Excel prior to pulling up a graph. When the user exits the Graph Menu form, I also use the same code.

If objExcel Is Nothing Then
Do Events
Else
objExcel.Quit
Set objExcel = Nothing
End If

Thanks for all of your replies. Thank you HobbitK for the code! It works perfectly now!
 
i realise you have an answer but ill throw this anyways, it was pointed out in a thread a few weeks back.

dim withevents xlobj as excel.application

this will give you a bunch of new events related to excel. you can then use

Private Sub xlobj_WindowDeactivate(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)

or

Private Sub xlobj_WorkbookDeactivate(ByVal Wb As Excel.Workbook)

which will fire when excel or a workbook is closed!

give at try if only for future projects!

hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top