vba 从excel vba关闭word应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41100661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:41:31  来源:igfitidea点击:

Closing word application from excel vba

vbaexcel-vbaexcel

提问by Tzvibe

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.

我试图在我的宏的开头关闭所有打开的 word 应用程序,尽管我不知道哪些文档是打开的,而且我无法将它们设置为对象。谢谢。

回答by Robin Mackenzie

This will close all running Word documents.

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

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

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

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

回答by Shai Rado

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

回答by brettdj

Another option is to use Shellto accesss the elegance of powershell

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

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