Word Automation - Winword.exe does not quit

Posted on 2009-02-10
Last Modified: 2012-05-06
I am requiring to write a ActiveX DLL which opens a Word instance on the server, opens a doc, prints it to various printers & trays, and then closes again.  The DLL is called from ASP on IIS.

I have stripped out all other code from the DLL and tested the following:

Function PrintDoc()

     Dim WordObj As Word.Application
     Set WordObj = New Word.Application
     Set WordObj = Nothing
End Function

The above leaves winword.exe running on the server, (in processes).  If I call the DLL from the ASP page again, it will leave another version of winword.exe running, and so and so on.

I have tried  "Set WordObj = CreateObject("Word.Application")" instead, but this produces the same result.

Have searched this site and google and cannot find an answer on why WordObj.Quit will not kill the winword.exe instance.
Question by:justinkent
    LVL 53

    Expert Comment

    Did you also set all the wordDocs, ... to nothing ?

    Author Comment

    Yes I did, but to try and solve the issue, I stripped out code bit by bit, to try and find the problem to find that, as above, simply creating an instance of word, and then trying to kill it does not work. Winword.exe remains running.

    I have also just installed the dll and script on another server to find the same fault, so it is not an issue with the server or the MS Office installation it seems.

    LVL 53

    Expert Comment

    If it's a big issue, you can use the following code

    Just make a module and put the code bellow.

    Then use this:
    call KillProcess("Winword.exe")

    Option Compare Database
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * 260
    End Type
    Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
    ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
    ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
    ByVal uExitCode As Long) As Long
    Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    Public Sub KillProcess(NameProcess As String)
    Const TH32CS_SNAPPROCESS As Long = 2&
    Dim uProcess  As PROCESSENTRY32
    Dim RProcessFound As Long
    Dim hSnapshot As Long
    Dim SzExename As String
    Dim ExitCode As Long
    Dim MyProcess As Long
    Dim AppKill As Boolean
    Dim AppCount As Integer
    Dim i As Integer
    Dim WinDirEnv As String
           If NameProcess <> "" Then
              AppCount = 0
              uProcess.dwSize = Len(uProcess)
              hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
              RProcessFound = ProcessFirst(hSnapshot, uProcess)
                i = InStr(1, uProcess.szexeFile, Chr(0))
                SzExename = LCase$(left$(uProcess.szexeFile, i - 1))
                WinDirEnv = Environ("Windir") + "\"
                WinDirEnv = LCase$(WinDirEnv)
                If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
                   AppCount = AppCount + 1
                   MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                   AppKill = TerminateProcess(MyProcess, ExitCode)
                   Call CloseHandle(MyProcess)
                End If
                RProcessFound = ProcessNext(hSnapshot, uProcess)
              Loop While RProcessFound
              Call CloseHandle(hSnapshot)
           End If
    End Sub

    Open in new window


    Author Comment

    Thanks Dhaest,

    Two things -

    1)  You've come accross this problem before?  If so, do you know why Winword.exe will not shutdown on the .Quit command?  

    2) It will be the case that multiple users will be running the script at any one time. Therefore, does your script kill every instance of winword.exe running, or only the single instance called by the ASP script?
    LVL 53

    Expert Comment

    Is Outlook running with Word as your Editor?

    Otherwise, what happens when you kill the winword.exe process? Does it come

    Author Comment

    Hi Dhaest,

    I can't get your code to work.   It will not compile.  

    I was asking whether your code is designed to kill every existing instance of MS Word.  If it is, then it is not what I need, as other users maybe running their own instance.
    LVL 53

    Expert Comment

    >> I was asking whether your code is designed to kill every existing instance of MS Word.  If it is, then it is not what I need, as other users maybe running their own instance.

    In that case, I wont use the code, because it's possible that there will be impact on other users.

    LVL 6

    Expert Comment

    An other problem could happen when terminating word when word requires an answer from user
    (e.g. to save a document before quitting or when there is a problem quitting while printing or a printing problem itself).
    Try WordObj.Visible=True before you quit to see the status of word before.

    Accepted Solution

    Thanks, tried that already but no, it did not work.

    What does seem to work I have discovered is:   WordObj.Quit False

    However, that has only led me onto another problem, which is a whole other story!  It seems that on the whole, MS Office is NOT designed to work programmatically on a server and will throw out errors and crash at every opportunity.

    Am looking at OpenOffice instead.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
    This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now