Problem closing Word form VB 
Author Message
 Problem closing Word form VB

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
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



Sun, 28 Mar 2004 10:02:16 GMT  
 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

- Show quoted text -

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

>.



Mon, 29 Mar 2004 17:39:27 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Closing a form and closing Word

2. problems using close [x] button when closing forms.

3. Print a Word document from VB and then Close Word

4. Word 8.0 and VB 5.0: object variable destroyed after closing word

5. Tip : VB.NET Form Won't Close on Me.Close + User Controls

6. Forms, primary thread closing when closing startup form

7. Form Close Button Does Not Close the Form

8. Closing form and Lossing Focus on Word

9. NT WS4.0/VB Form Unload Closes Down VB

10. move between Access and Word, back to Access, Close word

11. How to close word from within word

12. Closing and opening word from out vb

 

 
Powered by phpBB® Forum Software