VB6 Close a program

Posted on 2007-10-02
Last Modified: 2008-01-09
HI - I am using the below code to close and open a program.  The open portion works fine, however, the close portion is not working. TargetHwnd keeps getting a zero value.  Anyone know a better way of doing this? I am using ClickYes in order to suppress some Outlook prompts but I don't want to leave it running on the machine once the job is complete (for obvious reasons).

'API's Function Declarations

 Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long

 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long) As Long

 Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

 Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As Any, _
    ByVal lpWindowName As String) As Long

 'API Constants

 Public Const GWL_STYLE = -16

 Public Const WS_DISABLED = &H8000000

 Public Const WM_CANCELMODE = &H1F

 Public Const WM_CLOSE = &H10

 Public Function IsTaskRunning(sWindowName As String) As Boolean
    Dim hwnd As Long, hWndOffline As Long
    On Error GoTo IsTaskRunning_Eh
    'get handle of the application
    'if handle is 0 the application is currently not running
    hwnd = FindWindow(0&, sWindowName)
    If hwnd = 0 Then
        IsTaskRunning = False
        Exit Function
        IsTaskRunning = True
    End If

        Exit Function

    Call ShowError(sWindowName, "IsTaskRunning")

 End Function

 Public Function EndTask(sWindowName As String) As Integer
    Dim X As Long, ReturnVal As Long, TargetHwnd As Long
    'find handle of the application
    TargetHwnd = FindWindow(0&, sWindowName)
    If TargetHwnd = 0 Then Exit Function
    If IsWindow(TargetHwnd) = False Then
        GoTo EndTaskFail
    'close application
        If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
            X = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
        End If
    End If
    GoTo EndTaskSucceed

    ReturnVal = False
    MsgBox "EndTask: cannot terminate " & sWindowName & " task"
    GoTo EndTaskEndSub

    ReturnVal = True

    EndTask% = ReturnVal

 End Function

 Public Function ShowError(sText As String, sProcName As String)
   'this function displays an error that occurred
    Dim sMsg As String
    sMsg = "Error # " & Str(Err.Number) & " was generated by " _
         & Err.Source & vbCrLf & Err.Description
    MsgBox sMsg, vbCritical, sText & Space(1) & sProcName
    Exit Function

 End Function
Question by:gigglick
    1 Comment
    LVL 65

    Accepted Solution

    Have you made sure that your sWindowName is exactly correct, when passed to the FindWindow function?  It must be exactly the same title as that in the title bar of the application, which, for Microsoft Outlook, probably changes at each different view....



    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now