[英]Closing word application from excel vba
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.我试图在我的宏的开头关闭所有打开的 word 应用程序,尽管我不知道哪些文档是打开的,而且我无法将它们设置为对象。 Thanks.
谢谢。
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")
On Error Go To 0
If Not objWord Is Nothing Then
objWord.Quit
Set objWord = Nothing
End If
Loop Until objWord Is Nothing
End Sub
Edit编辑
Correction below because the loop above has a flaw ie it will exit after the first instance of Word is closed ...下面更正,因为上面的循环有一个缺陷,即它会在 Word 的第一个实例关闭后退出......
Option Explicit
Sub CloseWordDocuments()
Dim objWord As Object
Dim blnHaveWorkObj As Boolean
' assume a Word object is there to be quit
blnHaveWorkObj = True
' loop until no Word object available
Do
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
' quit loop
blnHaveWorkObj = False
Else
' quit Word
objWord.Quit
' clean up
Set objWord = Nothing
End If
Loop Until Not blnHaveWorkObj
End Sub
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
Another option is to use Shell
to accesss the elegance of powershell另一种选择是使用
Shell
来访问powershell的优雅
Sub Comesfast()
X = Shell("powershell.exe kill -processname winword", 1)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.