Close a Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rossmrn
    New Member
    • Feb 2007
    • 7

    Close a Program

    Hi
    I've opened a program using the code
    Dim RetVal
    RetVal = Shell("C:\Progr am Files\Outlook Express\msimn.e xe", vbNormalFocus)
    Then i create file's and report's and send them off which works fine.
    However now i want to close the Application, i've tried
    Dim objOutlook As Object
    Set objOutlook = CreateObject("O utlook.Applicat ion")
    objOutlook.Clos e
    but im being told "ActiveX component can't create object", am i missing an DLL have i gone the wrong about it all together?

    Any thoughts would be appreciated!

    Thanks
  • JConsulting
    Recognized Expert Contributor
    • Apr 2007
    • 603

    #2
    Originally posted by rossmrn
    Hi
    I've opened a program using the code
    Dim RetVal
    RetVal = Shell("C:\Progr am Files\Outlook Express\msimn.e xe", vbNormalFocus)
    Then i create file's and report's and send them off which works fine.
    However now i want to close the Application, i've tried
    Dim objOutlook As Object
    Set objOutlook = CreateObject("O utlook.Applicat ion")
    objOutlook.Clos e
    but im being told "ActiveX component can't create object", am i missing an DLL have i gone the wrong about it all together?

    Any thoughts would be appreciated!

    Thanks
    the shell command you're using simply launches the application...i t doesn't own it. You have two choices. You can use automation to "control" the application as it performs it's actions, or you can use your current method, but then you're going to have to delve into the world of killing PIDs Program ID numbers....much like going into the CTL-ALT-DEL and killing a process.
    J

    Comment

    • FishVal
      Recognized Expert Specialist
      • Jun 2007
      • 2656

      #3
      Originally posted by JConsulting
      the shell command you're using simply launches the application...i t doesn't own it. You have two choices. You can use automation to "control" the application as it performs it's actions, or you can use your current method, but then you're going to have to delve into the world of killing PIDs Program ID numbers....much like going into the CTL-ALT-DEL and killing a process.
      J
      You are right. The code below opens "notepad.ex e" and closes it via sending WM_CLOSE to all windows created by the process.

      Code:
      Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
      	 (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
      	 lParam As Any) As Long
       
      Public Declare Function GetWindowThreadProcessId Lib "user32" _
      (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
       
      Public Declare Function EnumWindows Lib "user32" _
      	 (ByVal fpCallback As Long, ByVal lParam As Long) As Long
       
      Public Const WM_CLOSE = 16
       
       
      Public Sub RunAndCloseApp()
       
      	Dim lngProcID As Long
       
      	lngProcID = Shell("notepad")
      	MsgBox "Notepad started, Ok to close it", vbOKOnly, "Close App"
      	CloseProcess lngProcID
       
      End Sub
       
       
      Public Sub clbEnumWindows(ByVal hWnd As Long, ByVal lngParam As Long)
       
      	Dim lngProcID As Long
       
      	GetWindowThreadProcessId hWnd, lngProcID
      	If lngProcID = lngParam Then
      		'Debug.Print hWnd, lngProcID
      		SendMessage hWnd, WM_CLOSE, 0, 0
      	End If
       
      End Sub
       
       
      Public Sub CloseProcess(ByVal lngProcID)
      	EnumWindows AddressOf clbEnumWindows, lngProcID
      End Sub

      Comment

      • rossmrn
        New Member
        • Feb 2007
        • 7

        #4
        I keep on getting caught up on things that i forget to thank the people that replied
        The above piece of code was just what i was looking for

        Thank you !

        Comment

        • FishVal
          Recognized Expert Specialist
          • Jun 2007
          • 2656

          #5
          Originally posted by rossmrn
          I keep on getting caught up on things that i forget to thank the people that replied
          The above piece of code was just what i was looking for

          Thank you !
          Thanks for response. I'm glad it was helpful for you.

          Comment

          Working...