Waiting for com program to terminate

  • Thread starter Thread starter Bob
  • Start date Start date
B

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