Final word app from excel vba

I try to close the entire text application at the beginning of my macro if it is open, although I don’t know which documents are open, and I cannot install them as an object. Thanks.

+3
source share
3 answers

This will close all Word documents.

You need On Error Resume Next to prevent errors if an instance of the Word application is not 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 
+3
source

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

 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 
+1
source

Another option is to use Shell to access elegance

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

Source: https://habr.com/ru/post/1486918/


All Articles