从excel vba关闭word应用程序

时间:2021-11-26 07:37:06

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 个解决方案

#1


3  

This will close all running Word documents.

这将关闭所有正在运行的Word文档。

You need On Error Resume Next to prevent errors if no Word application instance is running.

如果没有运行Word应用程序实例,则需要On Error Resume Next以防止出现错误。

Option Explicit

Sub CloseWordDocuments()

    Dim objWord As Object

    Do
        On Error Resume Next
        Set objWord = GetObject(, "Word.Application")
        If Not objWord Is Nothing Then
            objWord.Quit
            Set objWord = Nothing
        End If
    Loop Until objWord Is Nothing

End Sub

#2


1  

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

尝试下面的代码,它将关闭Word应用程序(不保存)。

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

#3


1  

Another option is to use Shell to accesss the elegance of

另一个选择是使用Shell来访问powershell的优雅

Sub Comesfast()
X = Shell("powershell.exe kill -processname winword", 1)
End Sub

#1


3  

This will close all running Word documents.

这将关闭所有正在运行的Word文档。

You need On Error Resume Next to prevent errors if no Word application instance is running.

如果没有运行Word应用程序实例,则需要On Error Resume Next以防止出现错误。

Option Explicit

Sub CloseWordDocuments()

    Dim objWord As Object

    Do
        On Error Resume Next
        Set objWord = GetObject(, "Word.Application")
        If Not objWord Is Nothing Then
            objWord.Quit
            Set objWord = Nothing
        End If
    Loop Until objWord Is Nothing

End Sub

#2


1  

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

尝试下面的代码,它将关闭Word应用程序(不保存)。

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

#3


1  

Another option is to use Shell to accesss the elegance of

另一个选择是使用Shell来访问powershell的优雅

Sub Comesfast()
X = Shell("powershell.exe kill -processname winword", 1)
End Sub