Results 1 to 4 of 4

Thread: Closing a Window

  1. #1
    Guest

    Post

    Is it possible for me to close an I.E. window for example through my Visual Basic code. I found where I can get a list of open windows, etc. but I can't find anything on closing a window. Thanks for your help!!

    --Shannon Hunter
    Aperture-CVO
    Information Systems
    shunter@aperture-cvo.com

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Post

    'close MSIE Browser
    '
    Option Explicit

    Private Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName _
    As String) As Long

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

    Private Const WM_CLOSE = &H10

    Private Sub Command1_Click()


    Dim MSIEhWnd As Long
    Dim RetVal As Long
    MSIEhWnd = FindWindow(vbNullString, "Microsoft Internet Explorer")
    RetVal = SendMessage(MSIEhWnd, WM_CLOSE, 0&, 0&)

    End Sub

  3. #3
    Guest

    Post

    Ok, I tried using your suggestions and it works perfectly for a Microsoft Excel Application. However when I put in the Microsoft Internet Explorer as you suggested it finds the windows handle, but it is not sending the 'Close' message. Any ideals??

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    OK, if that method doesn't work for you, try the TerminateProcess API which will Force a Process to Close, ie.
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF
    
    Private Sub Command1_Click()
        Dim lHwnd As Long
        Dim lProcess As Long
        Dim lExitCode As Long
        
        'Get the Window Handle
        lHwnd = FindWindow("IEFrame", vbNullString)
        'Get the ProcessID
        Call GetWindowThreadProcessId(lHwnd, lProcess)
        'Get the Process Handle
        lProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, lProcess)
        'Get the Exitcode
        Call GetExitCodeProcess(lProcess, lExitCode)
        'Terminate the Process
        Call TerminateProcess(lProcess, lExitCode)
    
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    ajyoung@pressenter.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width