Closing word application from excel vba

前端 未结 3 697
死守一世寂寞
死守一世寂寞 2021-01-21 00:34

I\'m trying in the beginning of my macro to close all word application if it\'s open, although I don\'t no which documents are open, and I can\'t set them as an object. Thanks.<

3条回答
  •  无人共我
    2021-01-21 01:17

    Try the code below, it will close Word Application (without saving).

    Option Explicit
    
    Sub CloseWordWindows()
    
    Dim objWord As Object
    
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    
    ' if no active Word is running >> exit Sub
    If objWord Is Nothing Then
        Exit Sub
    End If
    
    objWord.Quit
    Set objWord = Nothing
    
    End Sub
    

提交回复
热议问题