Private mstrMySubProcessName as String 'This will be the exact name of your sub process
Call KillProcess(mstrMySubProcessName)
Friend Sub KillProcess(ByVal ProcessName As String)
Dim prc() As Process
Dim ERROR_FILE_NOT_FOUND As Integer = 2
Dim ERROR_ACCESS_DENIED As Integer = 5
Try
prc = System.Diagnostics.Process.GetProcessesByName(ProcessName)
Dim eprc As IEnumerator = prc.GetEnumerator
eprc.Reset()
While eprc.MoveNext
Dim proc As Process = CType(eprc.Current, Process)
proc.Kill()
proc = Nothing
End While
eprc = Nothing
prc = Nothing
Catch e As System.ComponentModel.Win32Exception
If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
'MessageBox.Show(e.Message + ". Process not found.")
Throw New UnauthorizedAccessException(e.Message & ". Process not found.")
Else
If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
' Note that if your word processor might generate exceptions
' such as this, which are handled first.
'MessageBox.Show(e.Message + ". You do not have permission to kill this process.")
Throw New UnauthorizedAccessException(e.Message & ". You do not have permission to kill this process.")
End If
End If
End Try
End Sub