?
Solved

Close a program

Posted on 2003-02-26
7
Medium Priority
?
178 Views
Last Modified: 2010-05-01
Hi everbody,
I want to close a program (ex. MS Outlook) that is not started by my program. The only thing I know is the name of the program.
Have you an idea who I can do this?

Thanks
PDW
0
Comment
Question by:dwphilippe
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Steiner
ID: 8032076
Add a reference to the Outlook object library to your project.
Then add the following code to close 1 instance of Outlook:

Sub CloseOl()
   Dim ol As Outlook.Application
   
   On Error Resume Next
   Set ol = GetObject(, "Outlook.Application")
   If Err.Number = 429 Then MsgBox "Not found": Exit Sub
   ol.Quit
End Sub
0
 
LVL 11

Expert Comment

by:Steiner
ID: 8032079
Just forgot, put the follwing line right after ol.Quit:

Set ol = Nothing

(Just some cleanup)

Greets
Steiner
0
 
LVL 1

Accepted Solution

by:
anand2k earned 140 total points
ID: 8032439
i hope this code can help u
this will close any program that is running,

in the example calculator.exe is used
some api deeclarations r there and then use them

**************************
Private Declare Function WaitForSingleObject Lib "kernel32" _
         (ByVal hHandle As Long, _
         ByVal dwMilliseconds As Long) As Long

      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, _
         ByVal lParam As Long) As Long

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

      'Constants used by the API functions
      Const WM_CLOSE = &H10
      Const INFINITE = &HFFFFFFFF

      Private Sub Form_Load()
         Command1.Caption = "Start the Calculator"
         Command2.Caption = "Close the Calculator"
      End Sub

      Private Sub Command1_Click()
      'Starts the Windows Calculator
         Shell "calc.exe", vbNormalNoFocus
      End Sub

      Private Sub Command2_Click()
      'Closes the Windows Calculator
         Dim hWindow As Long
         Dim lngResult As Long
         Dim lngReturnValue As Long

         hWindow = FindWindow(vbNullString, "Calculator")
         lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
         lngResult = WaitForSingleObject(hWindow, INFINITE)

         'Does the handle still exist?
         If IsWindow(hWindow) = 0 Then
            'The handle still exists. Use the TerminateProcess function
            'to close all related processes to this handle. See the
            'article for more information.
            MsgBox "Handle still exists."
         Else
            'Handle does not exist.
            MsgBox "Program closed."
         End If
      End Sub
***************************************




0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dwphilippe
ID: 8032440
I think I am not clear enough. The program that must be close can be every program even a program that is write in VB, notepad, command sesion, ...
0
 
LVL 1

Expert Comment

by:anand2k
ID: 8032509
c, here on line

hWindow = FindWindow(vbNullString, "Calculator")

"Calculator", the 2nd parameter of findwindow api, must be the the title of the application that u want to close.

in ur case, MS Outlook, u can specify the exact window title of present in ms outlook window.


bye
Anand  :-)
       
0
 
LVL 1

Author Comment

by:dwphilippe
ID: 8032537
Yes Anand, that is whan I need. I go test it.
0
 
LVL 1

Author Comment

by:dwphilippe
ID: 8033759
It helps me a lot Anand2K.
I want to give your more points be I don't have them.

Thanks
PDW
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

621 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