Home All Groups Group Topic Archive Search About

Waiting for com program to terminate

Author
14 Aug 2006 3:19 PM
Bob
I launch Word from within my application (Vs2005, VB) and I would like to
make it necessary for the just opened Word to be closed before the rest of
my code in the calling sub can execute. How can I do this?

Thanks for any help
Bob

Author
15 Aug 2006 7:30 AM
R. MacDonald
Hello, Bob,

This isn't "elegant", but seems to work:

     Imports System.Threading.Thread
     Imports System.Runtime.InteropServices

     Public Class Form1
       Inherits System.Windows.Forms.Form
       .  .  .
       Private Sub Button1_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                               Handles Button1.Click
         Dim appWord As New Word.Application
         appWord.Visible = True
         MsgBox("Waiting for Word to close...")

         Do
           Try
             ' Examine some (any) property of Word application.
             Dim booTest As Boolean = appWord.Visible
             Sleep(100)

             Catch ex As COMException
               If (ex.ErrorCode = &H80010108) Then
                   ' object has disconnected from its clients.
                   MsgBox("Continuing...")
                   Exit Do
               Else
                   ' Unexpected error.
                   Throw New Exception(ex.Message, ex)
               End If

           Catch ex As Exception
             ' Unexpected error.
             Throw New Exception(ex.Message, ex)

           End Try
         Loop

       End Sub

     End Class

Cheers,
Randy


Bob wrote:

Show quoteHide quote
> I launch Word from within my application (Vs2005, VB) and I would like to
> make it necessary for the just opened Word to be closed before the rest of
> my code in the calling sub can execute. How can I do this?
>
> Thanks for any help
> Bob
>
>
Author
15 Aug 2006 7:51 AM
R. MacDonald
Hello again, Bob,

I've just seen your later post, and (if your staring Word as a process
rather than as an application) Cor's answer there is much better .  That is:

     Private Sub Button1_Click(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                                     Handles Button1.Click

       Dim procWord As Process = Process.Start("Mydoc.doc")

       MsgBox("Waiting for Word to close...")
       procWord.WaitForExit()
       MsgBox("Continuing...")

     End Sub

Cheers,
Randy


R. MacDonald wrote:

Show quoteHide quote
> Hello, Bob,
>
> This isn't "elegant", but seems to work:
>
>     Imports System.Threading.Thread
>     Imports System.Runtime.InteropServices
>
>     Public Class Form1
>       Inherits System.Windows.Forms.Form
>       .  .  .
>       Private Sub Button1_Click(ByVal sender As System.Object, _
>                                 ByVal e As System.EventArgs) _
>                                               Handles Button1.Click
>         Dim appWord As New Word.Application
>         appWord.Visible = True
>         MsgBox("Waiting for Word to close...")
>
>         Do
>           Try
>             ' Examine some (any) property of Word application.
>             Dim booTest As Boolean = appWord.Visible
>             Sleep(100)
>
>             Catch ex As COMException
>               If (ex.ErrorCode = &H80010108) Then
>                   ' object has disconnected from its clients.
>                   MsgBox("Continuing...")
>                   Exit Do
>               Else
>                   ' Unexpected error.
>                   Throw New Exception(ex.Message, ex)
>               End If
>
>           Catch ex As Exception
>             ' Unexpected error.
>             Throw New Exception(ex.Message, ex)
>
>           End Try
>         Loop
>
>       End Sub
>
>     End Class
>
> Cheers,
> Randy
>
>
> Bob wrote:
>
>> I launch Word from within my application (Vs2005, VB) and I would like
>> to make it necessary for the just opened Word to be closed before the
>> rest of my code in the calling sub can execute. How can I do this?
>>
>> Thanks for any help
>> Bob
>>