We help IT Professionals succeed at work.

Word Automation - Winword.exe does not quit

Medium Priority
2,077 Views
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
   
     WordObj.Quit
     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.
Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

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

Author

Commented:
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.

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
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
 
Type PROCESSENTRY32
    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 PROCESS_ALL_ACCESS = &H1F0FFF
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)
  
          Do
            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

Commented:
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?
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Is Outlook running with Word as your Editor?

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

Author

Commented:
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.
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
>> 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.

VK

Commented:
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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.