PDA

View Full Version : Problem with Application.Quit



blacktusk
06-18-2009, 06:56 PM
I have an Excel Application that is being automatically spawned from another Application. The Excel is running a report lookup and printout called from Sub Workbook_Open()

I dont believe that the details of the report are significant. Essentially if the workbook is opened with the /print option then the Printout occurs automatically and then closes the workbook. This all works fine, however the Excel application window will not close.


Private Sub Workbook_Open()


Dim CmdRaw As Long
Dim CmdLine As String
Dim doPrint As Long

Dim xlApp As Excel.Application
Set xlApp = Me.Application

CmdRaw = GetCommandLine
CmdLine = CmdToSTr(CmdRaw)
'MsgBox CmdLine

doPrint = InStr(CmdLine, "print")

'MsgBox "starting"
If doPrint > 0 Then 'Report Date is yesterday
xlApp.Sheets("report").Cells(1, 4).Value = Format(DateAdd("d", -1, Date), "yyyy-mm-dd")
Else
xlApp.Sheets("report").Cells(1, 4).Value = Format(Date, "yyyy-mm-dd")
End If
'ActiveSheet.Cells(2, 4).Value = Format(Date, "yyyy-mm-dd")
RunReport1 "A7"
If doPrint > 0 Then
xlApp.Sheets("report").PrintOut
'xlApp.ScreenUpdating = False
xlApp.DisplayAlerts = False
For Each Workbook In xlApp.Workbooks
Workbook.Close (False)
Next
xlApp.Quit
Set xlApp = Nothing
End If
End Sub

GTO
06-18-2009, 11:11 PM
Greetings BlackTusk,

Humbly, I believe you are setting xlApp to the current instance of the Application, rather than a new instance. If that was your goal, that is fine, although all this saves is typing a few letters (ie - xlApp instead of Application).

That said and still presuming you are wanting to work in the current instance, then this will not work:

For Each Workbook In xlApp.Workbooks
Workbook.Close (False)
Next

xlApp.Quit

... cuz you're closing ThisWorkbook before trying to quit the App.

Does that make sense?

Mark

blacktusk
06-21-2009, 04:32 PM
No the intent was to reference the current instance - however I see your point, once I close the workbook my script will stop executing and thats the end of it all! I have tested without the Close and it works fine, many thanks