Word Automation - Winword.exe does not quit

Posted on 2009-02-10
Medium Priority
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
  • 4
  • 4
LVL 53

Expert Comment

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

Author Comment

ID: 23599722
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

ID: 23599796
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 23599882
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

ID: 23599968
Is Outlook running with Word as your Editor?

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

Author Comment

ID: 23600041
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

ID: 23600055
>> 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.


Expert Comment

ID: 23620659
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

justinkent earned 0 total points
ID: 23621197
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 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