从Excel VBA关闭Word应用程序 [英] Closing word application from excel vba

查看:612
本文介绍了从Excel VBA关闭Word应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在宏的开头关闭所有word应用程序(如果打开的话),尽管我没有打开任何文档,也无法将它们设置为对象. 谢谢.

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文档.

This will close all running Word documents.

如果没有Word应用程序实例正在运行,则需要On Error Resume Next来防止错误.

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

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

这篇关于从Excel VBA关闭Word应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆