Connecting Tech Pros Worldwide Forums | Help | Site Map

Close a Program

Newbie
 
Join Date: Feb 2007
Posts: 7
#1: Jul 7 '07
Hi
I've opened a program using the code
Dim RetVal
RetVal = Shell("C:\Program Files\Outlook Express\msimn.exe", 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("Outlook.Application")
objOutlook.Close
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's Avatar
Expert
 
Join Date: Apr 2007
Location: Houston
Posts: 601
#2: Jul 7 '07

re: Close a Program


Quote:

Originally Posted by rossmrn

Hi
I've opened a program using the code
Dim RetVal
RetVal = Shell("C:\Program Files\Outlook Express\msimn.exe", 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("Outlook.Application")
objOutlook.Close
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...it 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
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#3: Jul 7 '07

re: Close a Program


Quote:

Originally Posted by JConsulting

the shell command you're using simply launches the application...it 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.exe" and closes it via sending WM_CLOSE to all windows created by the process.

Expand|Select|Wrap|Line Numbers
  1. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  2.      (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
  3.      lParam As Any) As Long
  4.  
  5. Public Declare Function GetWindowThreadProcessId Lib "user32" _
  6. (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
  7.  
  8. Public Declare Function EnumWindows Lib "user32" _
  9.      (ByVal fpCallback As Long, ByVal lParam As Long) As Long
  10.  
  11. Public Const WM_CLOSE = 16
  12.  
  13.  
  14. Public Sub RunAndCloseApp()
  15.  
  16.     Dim lngProcID As Long
  17.  
  18.     lngProcID = Shell("notepad")
  19.     MsgBox "Notepad started, Ok to close it", vbOKOnly, "Close App"
  20.     CloseProcess lngProcID
  21.  
  22. End Sub
  23.  
  24.  
  25. Public Sub clbEnumWindows(ByVal hWnd As Long, ByVal lngParam As Long)
  26.  
  27.     Dim lngProcID As Long
  28.  
  29.     GetWindowThreadProcessId hWnd, lngProcID
  30.     If lngProcID = lngParam Then
  31.         'Debug.Print hWnd, lngProcID
  32.         SendMessage hWnd, WM_CLOSE, 0, 0
  33.     End If
  34.  
  35. End Sub
  36.  
  37.  
  38. Public Sub CloseProcess(ByVal lngProcID)
  39.     EnumWindows AddressOf clbEnumWindows, lngProcID
  40. End Sub
  41.  
  42.  
Newbie
 
Join Date: Feb 2007
Posts: 7
#4: Jul 12 '07

re: Close a Program


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 !
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#5: Jul 12 '07

re: Close a Program


Quote:

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.
Reply