We help IT Professionals succeed at work.

terminate a programm and send vbreturn?

Falke asked

How can i terminate a programm (i know the title of the window) and send after termination a vbreturn, because if i terminate the programm he always ask me if i want to quit (for that the vbreturn)

Thanks in advanced

Watch Question


To send a return key use

SendKeys "{ENTER}"


i have tried it out. It's good, only 2 thinks:

in this code

Public Const WM_CLOSE =

WM_CLOSE IS UNDEFINIED! Which Value has it?

and second i know, only the begining of the "Titel" can i say "please search for the windows which beginns with "Test 5" or so?

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer

Hi Falke,

Take a look on the links:






Original Link: http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20168138

Here is the comment posted by TimCottee:

Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle

As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long,

ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule

As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule

As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As

Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Sub GetProcesses(strProcess As String)
 Dim lProcCount     As Long
 Dim lProcPoss      As Long
 Dim lNum           As Long
 Dim ProcessIDs()   As Long
 Dim cbNeeded2      As Long
 Dim NumElements2   As Long
 Dim arrModules(1 To 200) As Long
 Dim lRet           As Long
 Dim sModName       As String
 Dim hwndProcess    As Long
 Dim iCounter       As Long
 Const SIZE = 500
 'need to get the array containing the process id's for each process object
 lProcCount = 8
 lProcPoss = 96
 Do While lProcCount <= lProcPoss
     lProcCount = lProcCount * 2
     ReDim ProcessIDs(lProcCount / 4) As Long
     lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
 lNum = lProcPoss / 4
 For iCounter = 1 To lNum
     'Get a handle to the Process
     hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
     'Got a Process handle
     If hwndProcess <> 0 Then
         'Get an array of the module handles for the specified
         lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
         'If the Module Array is retrieved, Get the ModuleFileName
         If lRet <> 0 Then
             sModName = Space(255)
             lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
             'Form1.lstProcesses.AddItem ProcessIDs(iCounter) & vbTab & Left(sModName, lRet)
             If UCase(Left(sModName, lRet)) = UCase(strProcess) Then
                 TerminateProcess hwndProcess, 0&
             End If
         End If
     End If
     'Close the handle
     lRet = CloseHandle(hwndProcess)
End Sub

Example: GetProcesses "c:\Program Files\Microsoft Office\Winword.exe"

or Another simple example:

'Close an application using the API.
Private Declare Function FindWindow Lib "user32" Alias _

"FindWindowA" (ByVal lpClassName As String, _

ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias _

"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _

ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10
Private Sub cmdClose_Click()

    Dim winHwnd As Long

    Dim RetVal As Long

    winHwnd = FindWindow(vbNullString, Text1.Text)

    Debug.Print winHwnd

    If winHwnd <> 0 Then

        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)

        If RetVal = 0 Then

            MsgBox "Error posting message."

        End If


        MsgBox Text1.Text + " is not open."

    End If

End Sub

'Hope will help

with the first one i only have a question, where can i get:

psapi.dll (what's that?)

with the link you gave me, i have found another programm, that i can "Destroy" the programm with the Title. Is there no option to "quit" and send them a RETURN (because the programm ask if you want to quit -> have to get focus on the programm first!)



the last question from Falk2 has not been answered, would also want to know!


Why a C grade? I will consult a moderator to review this question. btw, the second question is a totally different question from the original one.

A request has been made to change the grade,  The expert feels the current grade does not fit the criteria of the intended guidelines.  Those guidelines are below.  If no response, I will review this question and grade accordingly..

E-E Moderator

How Do I Know What Grade to Give?
Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:

A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Remember, the Expert helping you today is probably going to be helping you next time you post a question. Give them a fair chance to earn an 'Excellent!' grade and they'll provide you with some amazing support.


you are right

i a "C" grade ist not right

i have given the points to the false expert

it should be "ryancys"

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer

So, if a wrong person is awarded. Is me deserve a 'C', anyway?


yes, because the code is not completed

where can i get the "psapi.dll"? Because the code doesn't work as i know, because the file is missing!

After review, question grade will be changed.  It seems the initial question was answered.

E-E Moderator


@Computer 101


Read the question carefully!

I asked "because if i terminate the programm he always ask me if i want to quit (for that the vbreturn)"

but this is not answerd. The Answer form the first person ist WITHOUT Answering this question!


Explore More ContentExplore courses, solutions, and other research materials related to this topic.