Close a Shell executable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlaverge
    New Member
    • Nov 2008
    • 1

    Close a Shell executable

    Hello,

    I executed a .exe file from excel using the Shell() function
    however, this program doesn't close on ending its calculations, so i want to close it from excel
    Anyone an idea how to do this?

    thank you in advance
    jelle
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Is this it?

    Not my code but try it, will stop back to see where you. Or you can simply hang on, our experts will you and perhaps will come aid.

    [CODE=VB]

    Option Explicit

    Private Sub Command1_Click( )
    'open and write to bat file
    Open "c:\test.ba t" For Output As #1
    Print #1, "dir c:\ >c:\test.txt"
    Close #1
    'create the file (test.txt) for output to be sent to
    Open "c:\test.tx t" For Output As #1
    Close #1
    'now shell to the bat file and then kill it
    Shell "c:\test.ba t", vbNormalFocus
    Kill "c:\test.ba t"
    End Sub

    [/CODE]

    Comment

    • jg007
      Contributor
      • Mar 2008
      • 283

      #3
      I have had a look but cannot work out how to get a handle to the program but I have the code below which uses an imported dll to close a window based on the title, at the moment it opens notepad and then closes it when you click 'OK'

      Code:
          Option Explicit
      
         Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) 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
         Private Const WM_QUIT = &H12
         
         Private Sub CommandButton1_Click()
         Shell ("Notepad.exe")
         MsgBox ("Click 'OK' to close notepad")
          Dim sTitle As String
           Dim iHwnd As Long
           Dim ihTask As Long
           Dim iReturn As Long
           sTitle = "Untitled - Notepad"
           iHwnd = FindWindow(0&, sTitle)
           iReturn = PostMessage(iHwnd, WM_QUIT, 0&, 0&)
           MsgBox "Notepad has been closed down"
        End Sub

      Comment

      Working...