COM/OLE: How to test if winword.exe is started on the server?

…or: Why won’t this code error?

The object is to start winword.exe on the server, but only if there isn’t a copy already running. Then leave it running for the next time the agent is executed.

Here’s my (non-working) attempt. The problem I’m having is that the first createObject(“Word.Document”) is not throwing an error.

'If MS Word is not started the following create object SHOULD throw an error.

On Error Goto processActWrd

Set docObj = createObject(“Word.Document”)

On Error Goto process

Exit Function

processActWrd:

Set wrdObj = createObject(“Word.Application”)

Resume Next

Subject: COM/OLE: How to test if winword.exe is started on the server?

You probably must loop through all active Windows processes using WIN32 API from LotusScript to find out if winword is already started.

Subject: COM/OLE: How to test if winword.exe is started on the server?

Excellent answers all. Thanks.

Now for Part II (this question might be better on a COM/OLE board somewhere, but I don’t know any, so you all hafta help me ).

Why can’t I make MS Word quit until after I Kill the file it was editing?

In other words, this code does not work:

Call docObj.SaveAs (filepath+"\"+docfn$)

Call docObj.Close

Call wrdObj.Quit	<-- this line fails

But this code works:

Call docObj.SaveAs (filepath+"\"+docfn$)

Call docObj.Close

Kill filepath+"\"+docfn$

Call wrdObj.Quit

The only difference is that I’m killing the file that I was working on. (Note that this file is the function’s output, so Killing it isn’t such a great long-term solution.)

Thanks for any help.

-Jeff

Subject: RE: COM/OLE: How to test if winword.exe is started on the server?

So, you’re saying that deleting your work is a problem? :o)

I wonder if there mightn’t be a timing issue going on here. That is, Word hasn’t finished saving when you call Quit. Can you sleep for a second or so?

Subject: RE: COM/OLE: How to test if winword.exe is started on the server?

I added a DoEvents before application.Quit. It seems to be working for now – soon I’ll read about it and find out what it does.

Thanks for your help.

-Jeff

Subject: COM/OLE: How to test if winword.exe is started on the server?

Hi,

try the following:

…

On Error Resume Next

Set objWord = GetObject(“”, “Word.Application”)

If Err = 208 Then 'Error 208 occurs when Word not running

Err = 0

Set objWord = CreateObject “Word.Application”)

End If

…

Regards

Axel

Subject: RE: COM/OLE: How to test if winword.exe is started on the server?

I’d rather see:

[Edited for early-morning grogginess]

Dim IsFirstTry As Boolean

IsFirstTry = True

On Error Goto HandleError

set objWord = GetObject(“”,“Word.Application”)

.

.

.

GetOut:

Exit Sub

HandleError:

Select Case Err

Case 208

If IsFirstTry Then

IsFirstTry = False

Set objWord = CreateObject(“Word.Application”)

'optionally log a Word-not-running message

Resume Next

Else

'Log create Word problem

Resume GetOut

End If

Case Else

'Log stuff & quit

Resume GetOut

End Select

End Sub

A global On Error Resume Next is just about the worst sort of error handling – a good argument can be made for it being worse than no error handling at all, since there is no indication what the run-time problem was.

Subject: RE: COM/OLE: How to test if winword.exe is started on the server?

Oh, yeah – including LSERR.LSS or creating a corresponding constant yourself and then using the constant name rather than the “magic number” 208 would make the code more readable and maintainable.