Closing out a program

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152
Hello everyone,

I am using VBA to open a program
Code:
Shell ("C:\Program Files\PDFCreator\PDFCreator.exe")
and now I need VBA to close the same program and I can not find anything on the internet. Any help is greatly appreciated.
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152
This seems much more difficult than it is to open a program. I cant get anything to work.
 

tweedle

Well-known Member
Joined
Aug 1, 2010
Messages
1,559
If you don't want to get into hWnd, you can use command line again to kill the process:

Example:

Code:
Dim retval As Long                'In Declarations
Code:
Sub Main()
    Call StartProcess
    Call EndProcess
End Sub
Code:
Function StartProcess()
    retval = Shell("notepad.exe")
End Function
Code:
Function EndProcess()
    ' The /F makes it an immediate kill
    ' /PID = Process ID from the Shell command
    CMD$ = "taskkill /F /PID " & retval   
    dblRetVal = Shell(CMD$, vbHide)
End Function
fyi: You could also use CreateObject within your VBA and not be dependent on command line.
 

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152

ADVERTISEMENT

I found the code below but cant get it to work. Is there anything I am doing wrong or possibly an easier way of closing a .exe program such as pdfCreator?

Code:
Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szexeFile As String * 260
    
End Type

Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long

Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long

Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long

Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long

Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long

Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long


Public Sub KillProcess(NameProcess As String)
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const TH32CS_SNAPPROCESS As Long = 2&
Dim uProcess  As PROCESSENTRY32
Dim RProcessFound As Long
Dim hSnapshot As Long
Dim SzExename As String
Dim ExitCode As Long
Dim MyProcess As Long
Dim AppKill As Boolean
Dim AppCount As Integer
Dim i As Integer
Dim WinDirEnv As String
        
       If NameProcess <> "" Then
          AppCount = 0

          uProcess.dwSize = Len(uProcess)
          hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
          RProcessFound = ProcessFirst(hSnapshot, uProcess)
  
          Do
            i = InStr(1, uProcess.szexeFile, Chr(0))
            SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            WinDirEnv = Environ("Windir") + "\"
            WinDirEnv = LCase$(WinDirEnv)
        
            If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
               AppCount = AppCount + 1
               MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
               AppKill = TerminateProcess(MyProcess, ExitCode)
               Call CloseHandle(MyProcess)
            End If
            RProcessFound = ProcessNext(hSnapshot, uProcess)
          Loop While RProcessFound
          Call CloseHandle(hSnapshot)
       End If

End Sub
 

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152
Thanks tweedle I tried your code but it doesnt close the program. I guess because the program opens up into a start up icon? So I may need different code to close this startup icon?
 

tweedle

Well-known Member
Joined
Aug 1, 2010
Messages
1,559

ADVERTISEMENT

It's possible that might take more time to fully load than notepad.
Might try stepping through my posted code to slow things down a bit?
VBA doesn't necessarily wait for a shell process to complete.
sample code emulates going to task manager and killing a process.
I don't have a good grasp of what "program opens up into a start up icon" is.
 

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152
Ok, I went to the task manager and closed it that way and it closes it from the startup icons. So that must not be the case. There must be some other reason your code did not work for me.
 

MightyQuinn

Board Regular
Joined
Dec 28, 2007
Messages
152
I tried your code exactly in a new workbook and it opens the notepad but never closes it, even when I step through it slowly.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,712
Messages
5,512,988
Members
408,928
Latest member
Black Vinyl

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top