
Close application when workbook is closed
I need some help on this one. I'm trying to write a
script in VB that will close the Excel application when
the workbook is closed. But, I only want it to run if the
application was opened with the script. If the user has
other Excel workbooks open, then I don't want the script
to close the App. I'm pretty close, I think. I've got it
to open the application and close it based on the
variables listed above. However, it doesn't wait for the
workbook to close. Here's what I've got so far.
Function IsExcelRunning() As Boolean
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
IsExcelRunning = (Err.Number = 0)
Set xlApp = Nothing
Err.Clear
End Function
Private Sub Command96_Click()
If Me!Frame35 = 1 Then
strInput = InputBox("ENTER THE PASSWORD: ")
If strInput <> "PERCEPTA" Then
MsgBox "Sorry, you've entered an incorrect password."
Else
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim ExcelRunning As Boolean
ExcelRunning = IsExcelRunning()
If Not ExcelRunning Then
Set xlApp = CreateObject("Excel.Application")
Else
Set xlApp = GetObject(, "Excel.Application")
End If
xlApp.Visible = True
xlApp.Workbooks.Open ("\\usmlbfp1\library\change mgt
logs\axz\axz change log.xls")
End If
{THIS IS WHERE I NEED HELP. I DON'T WANT IT TO JUST OPEN
AND CLOSE. I NEED THE CODE TO MAKE THE APPLICATION CLOSE
ONLY AFTER THE WORKBOOK IS MANUALLY SHUT DOWN BY THE USER}
'If we started the instance, our code uses the
'Quit method to close the instance
If Not ExcelRunning Then xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End If
End Sub