Close Window

  • Thread starter Thread starter mark
  • Start date Start date
M

mark

Is it possible to close a programme window, after opening it with the shell
command in VBA?.
 
Mark,

It depends on the program. One method that may work is to send a
CLOSE message to the window.


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 FindWindow Lib "user32" Alias
"FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Sub CloseProgram()
Dim HWND As Long
Const WM_CLOSE = &H10

HWND = FindWindow(vbNullString, "Calculator") '<<<<
If HWND > 0 Then
SendMessage HWND, WM_CLOSE, 0&, 0&
End If
End Sub

Change the "Calculator" in the line marked with <<< to the
caption of the program window you want to close.
 
Thank you for your help with the code, runs fine and executes the line
"SendMessage HWND, WM_CLOSE, 0&, 0&"
but the windows doesn't close. The window I am trying to close is a web page
 
Back
Top