how to close Word opened by VBA

Asked

Viewed 1,724 times

2

For each Execution of the Deletesel function, a "WINWORD" is open in memory, even with the Close and/or Quit command it does not close.

Sub DeleteSel(msg As Outlook.MailItem)
Dim objDoc As Object
Dim objBkm As Object

Set objDoc = CreateObject("Word.Application")

objDoc.Visible = True
objDoc.Activate

On Error Resume Next
Set objDoc = msg.GetInspector.WordEditor
Set objBkm = objDoc.Bookmarks("_MailAutoSig")

If Not objBkm Is Nothing Then
    objBkm.Select
    objDoc.Windows(1).Selection.Delete
End If

objDoc.Close
objBkm.Close
Set objBkm = Nothing
Set objDoc = Nothing
End Sub

Would anyone have any tips ?

3 answers

1

Quit and Set Word object = Nothing

You create two objDoc with Set objDoc. Then at the end you close only the last, while the first remains open.

A VBA program to close all Word files is as follows:

'https://stackoverflow.com/a/41100852/7690982
Option Explicit

Sub FecharDocWord()

    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

Credits: Robin Mackenzie

Kill the task

To close processes in Task Manager.

'http://vbadud.blogspot.com.br/2009/04/how-to-kill-word-process-using-vba.html
Sub Kill_Word()

Dim sKillWord As String 

sKillWord = "TASKKILL /F /IM Winword.exe" 

Shell sKillWord, vbHide 

End Sub
Read more at http://vbadud.blogspot.com/2009/04/how-to-kill-word-process-using-vba.html#yWeSjbJsIeyDBDDv.99
  • 1

    Even if QUIT works, I could not use this feature, because if the user really is with Word Open, would close the user’s job.

  • 1

    Cool this option @danieltakeshi, thanks!!

1

I found a solution, which at least Opens only 1 instance of WINWORD in the task manager or Uses the one that is already open, since it is not closing with the QUIT command due to the use of Bookmarks (probably)

Sub DeleteSel(msg As Outlook.MailItem)
    Dim objDoc As Object
    Dim objBkm As Object

    On Error Resume Next
    Set objDoc = GetObject(, "Word.Application")

    If objDoc Is Nothing Then
        Set objDoc = CreateObject("Word.Application")
    End If

    On Error Resume Next
    Set objDoc = msg.GetInspector.WordEditor
    Set objBkm = objDoc.Bookmarks("_MailAutoSig")

    If Not objBkm Is Nothing Then
        objBkm.Select
        objDoc.Windows(1).Selection.Delete
    End If

    objBkm.Quit
    objDoc.Quit
    Set objBkm = Nothing
    Set objDoc = Nothing
End Sub
  • Friend, if the answer to your question has been answered by our answer, mark as answered for future reference of other users.

1

Use the option below:

objDoc.Quit

Instead of ". Close"

Before "Quit" save what you need to save.

Issue 1

Example

Sub abreWord()

Dim objDoc As Object

Set objDoc = CreateObject("Word.Application")

    objDoc.Visible = True
    objDoc.Activate

    objDoc.Quit

    Set objDoc = Nothing

End Sub

In the above case the word is open and closed, including the Winword process.

I hope I’ve helped!

  • WINWORD Object continues in Windows Manager occupying memory.

  • Though you put Set objDoc = Nothing? Which Office version you use?

  • Yes, even with Quit or Close the WINWORD object is in the tafera manager. Office 2016.

  • Friend, try to give one quit in the objBkm also, because in the code I will update in response the Winword is closed normally. I will update now.

  • If possible test this example and see if it is opening and closing there on your machine. Here tb I am in 2016 and it worked.... strange....

  • And I saw in the comment of the other reply... in fact here will close all instances of the open word.... if the user has something open will give the close command as well. This may be a problem. So I mentioned the need to save before. If you find another better post solution to leave the record and we learn how to do. Thank you!

  • So the problem should be Bookmarks, because even if quit the two remains open.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.