Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Youngstown, Ohio, USA
    Posts
    705
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Stop a running program (VBA 6.0)

    I want to be able to stop a running program/application from within a VBA function. I've found a number of ways to start a program but none that can end a program. What I want to do is something like this:

    <pre>If varCondition = ConditionMet Then
    CommandToStopRunningProcess ProgramName.exe
    End If</pre>

    Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Stop a running program (VBA 6.0)

    Do you want to close an Office application such as Word or Excel?
    If not, do you know the exact caption of the application window?

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Stop a running program (VBA 6.0)

    The brute force method is something like:
    <pre>Sub FindAndTerminate(ByVal strProcName As String)
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strList
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" _
    & strComputer & "rootcimv2")
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcName & "'")
    If colProcess.Count > 0 Then
    For Each objProcess In colProcess
    objProcess.Terminate
    Next objProcess
    End If
    End Sub
    </pre>

    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Youngstown, Ohio, USA
    Posts
    705
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Stop a running program (VBA 6.0)

    My immediate desire is to be able to stop 'Batchrun.exe' - a batch control program that actually launches the Access program this bit of code would be used in. I want to test a condition, and then based on the results stop Batchrun and keep it from executing any more commands. Batchrun is set to run windowless when it is executing a batch, but in edit mode the window caption is 'Batchrun'.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Stop a running program (VBA 6.0)

    OK, see if Rory's code works for you (the things I had in mind won't help for your situation)

  6. #6
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Youngstown, Ohio, USA
    Posts
    705
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Stop a running program (VBA 6.0)

    Works nice, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •