使用Excel VBA,如何关闭(保存后)我的计算机上当前打开的所有Microsoft Word文件(.docx)实例?这将是按下按钮。
我有这个代码可以关闭一个。我怎样才能使它适应未知数字呢?
Private Sub CommandButton4_Click()
Dim objWord As Object
Do Until GetObject(, "Word.Application") Is Nothing
Set objWord = GetObject(, "Word.Application")
Debug.Print objWord
objWord.Quit False
Loop
End Sub
答案 0 :(得分:0)
我对你的问题并不完全清楚,但是这段代码会遍历Word的多个实例(与多个文档不同)关闭该实例中的所有文档而不提示保存。它经过了轻微的测试:
Private Sub CommandButton4_Click()
Dim objWord As Object
'avoid error if no Word instances are open
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
On Error GoTo 0
Do Until objWord Is Nothing
Debug.Print objWord
objWord.Quit False
'start from scratch
Set objWord = Nothing
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
On Error GoTo 0
Loop
End Sub