?
Solved

Close a program

Posted on 2003-02-26
7
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

765 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