Search code examples
vbams-word

word is not closing via vba


when i open word, only UserForm1 is opened and word view is in the background. So far it works. When I close UserForm1, word also closes, all good but in the background in task manager microsoft word is still open and I don't want that. This means that when I close UserForm1, the complete word document should also be closed as normal.

my code:

Sub AutoOpen()

    Application.Visible = False

    UserForm1.Hide
    UserForm1.Show vbModeless
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer, appWord As Object)

    If CloseMode = vbFormCode Then
        ThisDocument.Close SaveChanges:=wdDoNotSaveChanges
        appWord.Quit
    End If
End Sub

Solution

  • Please, try UserForm_Terminate event in the next way:

    Private Sub UserForm_Terminate()
       Dim appWord As Object
       Set appWord = Application
       ThisDocument.Save
       appWord.Quit
    End Sub
    

    If the document having the form is closed, the part of the code quitting the application is not accesibile, anymore. In this way, the event is able to quit Application, which closes the workbook (without asking about saving it)...