• Stop a running program (VBA 6.0)

    Author
    Topic
    #440855

    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:

    If varCondition = ConditionMet Then
    	CommandToStopRunningProcess ProgramName.exe
    	End If

    Thanks.

    Viewing 1 reply thread
    Author
    Replies
    • #1057084

      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?

      • #1057098

        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’.

        • #1057100

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

    • #1057085

      The brute force method is something like:

      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
      
    Viewing 1 reply thread
    Reply To: Stop a running program (VBA 6.0)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: