Start and Terminate an External Application

Hi there, I'm after a way of starting and terminating an external application using Visual Basic 6.  i don't need to do anything fancy like wait until the application has closed or anything like that, simply start the application and then close it.  However, when I close it I'd like to terminate the application completely, e.g. like with word if you've changed the document when you click the X it asks you if you wish to save the changes, the application I'm closing works similar, it basically asks you if you're sure you wish to close it, i'd like to bypass this when closing the application from VB and just terminate it.

Any help is greatly  appreciated, cheers.
Who is Participating?
ShauliConnect With a Mentor Commented:
Locate two command buttons on your form, and copy this code:

'to start an application:
Private Sub Command1_Click()
Shell "yourpath&file.exe", vbMaximizedFocus
End Sub

'to terminate:
Public Function EndProcess(ByVal sbFilename As String) As String
    Dim Process As Variant
    EndProcess = "Can't terminate process :("
    For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process")
        If LCase(Process.Name) = LCase(sbFilename) Then
            EndProcess = Process.Name & " Terminated once and for all!"
        End If
End Function

Private Sub Command2_Click()
    MsgBox EndProcess("applicationnameonly.exe")   'just the application name, without the path
End Sub


Shiju SasidharanAssoc Project ManagerCommented:
'Code to Start an application
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1
Private Const SW_HIDE = 0
Private Sub Command4_Click()
    StartApplication ("C:\winnt\notepad.exe")
End Sub

Private Sub StartApplication(ByVal ApplicationPath As String)

    Call ShellExecute(hWnd, "open", ApplicationPath, "", "", SW_MAXIMIZE)

End Sub

'Code to End an aplication



Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Shiju SasidharanAssoc Project ManagerCommented:
ShelfieldCollegeAuthor Commented:
Thanks, your's seems to be the only one I could get working, I'm sure the others worked fine it's probably something I was doing wrong.  On a plus not your's will terminate every instance of that program which works out handy for a different projects I'm using so thanks again


Glad it works for you.

All Courses

From novice to tech pro — start learning today.