
Problem closing Word form VB
Hello,
One solution is to store the instance of MS-Word in a
variable. For example:
Public g_winwordObject As Word.Application
and to test if the instance is still existing with taking
a method of the object Word.Application. For example, the
method Name. Here is an exemple:
Private Function IsWordRunning() As Boolean
Dim applicName As String
On Error GoTo ErrorHandler
IsWordRunning = True
applicName = g_winwordObject.Name
Exit Function
ErrorHandler:
'Err 462 is expected ("The remote server machine does
not exist or is unavailable")
'restore memory
Set g_winwordObject = Nothing
'return value
IsWordRunning = False
End Function
I hope that the solution helps.
Luc Pouly
Quote:
>-----Original Message-----
>Hello,
>I have a problem when I try, from my VB6 application, to
close the instance
>of MS-Word I use for automation.
>I have a global Word.Application variable which holds the
instance of Word.
>When I want to close my application, for example, I want
it to close the
>running instance of Word. So I check it has been already
closed or not by
>using the following function:
>Function IsWordRunning() As Boolean
> Dim o As Word.Application
> On Error GoTo err_handler:
> IsWordRunning = False
> Set o = GetObject(, "Word.application")
> If Len(o) = 0 Then Exit Function
> IsWordRunning = True
> Exit Function
>err_handler:
> Exit Function
>End Function
>I close it if the function returns true.
>It works fine when I only have one instance of Word
running. But when I have
Quote:
>more, let's say I have one lauched by my application, and
another one
>launched manually. Let's say I close manually the one
launched by my
>application. The Word.Application variable is not linked
anymore to the
>instance of Word. The problem is that it looks like the
function
>"IsWordRunning" above thinks it is always running
(probable because
>GetObject returns any Word application running).
>How can I do to solve my problem ?
>Thanks a lot for any help.
>Fabien SPAGNOLO
>.