Link to home
Create AccountLog in
Avatar of jpp1jpp1
jpp1jpp1

asked on

MS Word as a reporting tool. Winword.exe doesn't close

Title is pretty self-explanatory.

I'm using version 11 in my project's reference section (Vbasic 6.0) although, with the purpose to get an all-version compatible program, I'm actually using late-binding:

    Dim WordApp As Object
    Dim WordDoc As Object

    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = CreateObject("Word.Document")


When the word template is filled I do the following for closing:

    WordDoc.Close
   
    WordApp.Quit
    Set WordDoc = Nothing
    Set WordApp = Nothing

But the winword.exe process isn't killed. In fact after generating several reports the same number of instances remain open, consuming more and more resources (loke 10M per instance)

Am I missing someting?

Ty in advance.
Avatar of jpp1jpp1
jpp1jpp1

ASKER

I just found out that my code is creating 2 instances of winword.exe thus .quit only closes one of them, but why?
Don't do createobject to get the document, do WordApp.Documents.Add:

    Dim WordApp As Object
    Dim WordDoc As Object

    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = WordApp.Documents.Add

    '...

    WordDoc.Close
   
    WordApp.Quit
    Set WordDoc = Nothing
    Set WordApp = Nothing

Oops, I'm ashamed there was a second call to

      Set WordApp = CreateObject("Word.Application")

in my code. Sorry :P
What's the difference with WordApp.Documents.Add?
ASKER CERTIFIED SOLUTION
Avatar of jmundsack
jmundsack
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Ty for helping.