3
votes
If (Documents.count = 1) And (ActiveDocument.Name = ThisDocument.Name) Then
    Application.DisplayAlerts = False
    Application.Quit (wdDoNotSaveChanges)
Else
    Application.DisplayAlerts = False
    ActiveDocument.Close (wdDoNotSaveChanges)
End If

I'm having issues with the above code, in that when the code clears that first If statement (meaning it is the only document open and the name of the document is as expected), it still won't close out of the Word Application completely. It instead will only close the document and then leave a "blank" Word window open.

I know it is clearing the first If statement because I wrote a quick check of each element to a debug file, and everything shows up as expected. Additionally, if I step through the code it indeed moves along as it should.

Interestingly/Frustratingly if I step through the code in debug mode and get to the section in the code for Application.Quit, it does indeed quit the entire program! So I'm really not sure why it doesn't work when I just run the code as opposed to stepping through it.

Have tried:

1 - Adding an 'Exit Sub' line after Application.Quit

2 - Setting the Word Application as an object explicitly:

Dim wObj  As Object
Set wObj = CreateObject("word.Application")
'Application.Quit (wdDoNotSaveChanges) '
wObj.Quit (wdDoNotSaveChanges)
Set wObj = Nothing

3 - Adding a before close event:

Sub DocumentBeforeClose()
ActiveDocument.Saved = True
End Sub

Any help would be much appreciated!

2
If you are using Late Binding it's likely the enum for wdDoNotSaveChanges isn't being found. I don't think you need to use this, just use ActiveDocument.Close False. Same thing for the Application, Application.Quit False - Ryan Wildry
Can you explain what the if condition should check? And can you also explain what ThisDocument is? It seems you run into the else-branch. - Dirk Vollmar
I can't replicate this - both code branches function as expected, both from the macro menu and in the debugger. Is this in an event handler? - Comintern
@DirkVollmar ThisDocument is the Word VBA object reference to the document containing the VBA code being executed. Essentially, if there is only one document open and it's the one with the code, the sub should Quit word, otherwise it should just close the open document - Dave
@Dave: Thanks, for the explanation, but I wanted to know what ThisDocument is in this specific case. I wasn't very clear about that, sorry... - Dirk Vollmar

2 Answers

0
votes

When having this issue in Excel, I found I had to tell Excel to close all documents after calling Application.Quit.

  • Because Excel can retain a reference to any workbook after the workbook's window closes (workbook still present in VBA Editor, unreleased object references)…
  • Because I can't always know that my document is the first document that has been opened by this instance of Excel…
  • Because I must accept the presence of an Excel add-in that can cause workbooks to appear modified immediately after a save (due to changes to invisible worbook properties)…
  • Because my usecase requires Excel to quit…

Adapted for Microsoft Word:

'Actual handling of unsaved documents omitted from this answer
If savedAllDocuments Or UserDoesNotCare Then
    Application.Quit wdDoNotSaveChanges
    Dim doc As Document
    For Each doc in Application.Documents
        doc.Close SaveChanges:=False
    Next doc
End If
0
votes

I had a simular issue with Application.Quit (quitting Word from within an Excel macro). It turned out, that it was related to the status of Application.ScreenUpdating. The macro hung when in Excel ScreenUpdating was set to false, but continued correctly with:

Application.ScreenUpdating = True