How to Close an application?

A

Avi

Hello,

I open an application via this code thaht works fine
Sub LaunchCaptureExpress()
Dim RetVal As Long
RetVal = Shell("C:\Program Files\Capture
Express\capexp.exe", vbNormalFocus)
End Sub

How do i close it???

Thanks a lot


Avi
www.avibenita.com
 
C

chijanzen

Hi ,Avi

try it!

close the Windows after 10 seconds

Sub LaunchCaptureExpress()
Dim WshShell As Object
Dim oExec As Object
Set WshShell = CreateObject("Wscript.Shell")
Set oExec = WshShell.Exec("C:\Program Files\Capture Express\capexp.exe")
DoEvents
Application.Wait (Now + TimeValue("0:00:10"))
oExec.Terminate
Set WshShell = Nothing
Set oExec = Nothing
End Sub
 
A

Avi

Hi,

Thanks for your prompt answer

When doing what you advise, I get an error 'activex can't create object'.
I've added a reference, but the same error remains


Please help

Avi

Avi Benita 054-4660641 wwwAvi Benita 054-4660641 www.avibenita.com
 
R

RB Smissaert

This code will do it:

Public Declare Function GetDesktopWindow Lib "user32" () As Long

Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long

Public Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5
Const WM_CLOSE = &H10

Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" (ByVal hwnd As
Long, _
ByVal wMsg As
Long, _
ByVal wParam As
Long, _
lParam As Any) As
Long


Private Declare Function getWindowsDirectory _
Lib "kernel32" _
Alias "GetWindowsDirectoryA" (ByVal lpBuffer
As String, _
ByVal nSize As
Long) As Long

Function FindWindowHwndLike(hWndStart As Long, _
ClassName As String, _
WindowTitle As String, _
level As Long, _
lHolder As Long) As Long

'finds the first window where the class name start with ClassName
'and where the Window title starts with WindowTitle, returns Hwnd
'----------------------------------------------------------------
Dim hwnd As Long
Dim sWindowTitle As String
Dim sClassName As String
Dim r As Long

'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
If level = 0 Then
If hWndStart = 0 Then
hWndStart = GetDesktopWindow()
End If
End If

'Increase recursion counter
level = level + 1

'Get first child window
hwnd = GetWindow(hWndStart, GW_CHILD)

Do Until hwnd = 0
'Search children by recursion
lHolder = FindWindowHwndLike(hwnd, _
ClassName, _
WindowTitle, _
level, _
lHolder)

'Get the window text
sWindowTitle = Space$(255)
r = GetWindowText(hwnd, sWindowTitle, 255)
sWindowTitle = Left$(sWindowTitle, r)

'get the class name
sClassName = Space$(255)
r = GetClassName(hwnd, sClassName, 255)
sClassName = Left$(sClassName, r)

If InStr(1, sWindowTitle, WindowTitle, vbBinaryCompare) > 0 And _
sClassName Like ClassName & "*" Then
FindWindowHwndLike = hwnd
lHolder = hwnd
Exit Function
End If

'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)

Loop

FindWindowHwndLike = lHolder

End Function

Function CloseApp(ByVal strApp As String, _
ByVal strClass As String) As Long

'will find a window based on:
'the partial start of the Window title and/or
'the partial start of the Window class
'and then close that window
'for example, this will close Excel:
'CloseApp "", "XLM" and this will:
'CloseApp "Microsoft Excel", ""
'but this won't: CloseApp "", "LM"
'it will only close the first window that
'fulfills the criteria
'will return Hwnd if successfull, and 0 if not
'---------------------------------------------

Dim hwnd As Long

On Error GoTo ERROROUT

hwnd = FindWindowHwndLike(0, _
strClass, _
strApp, _
0, _
0)

If hwnd = 0 Then
CloseApp = 0
Exit Function
End If

'Post a message to the window to close itself
'--------------------------------------------
PostMessage hwnd, WM_CLOSE, 0&, 0&

CloseApp = hwnd

Exit Function
ERROROUT:

On Error GoTo 0
CloseApp = 0

End Function


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top