The
CloseWindow sub procedure in the following code sample uses the
SendMessage function to send a SC_CLOSE message to the Internet Explorer window, which immediately closes the Internet Explorer instance.
Keep in mind that the recommended method to close a Visual Basic document is to navigate the Internet Explorer instance away from the Visual Basic document to another resource.
Step-by-Steps Example
- Create a new ActiveX Document DLL project in Visual Basic.
- On the Project menu, click References, select Microsoft Internet Controls, and then click OK.
- Paste the following code into the User Document code window:
Option Explicit
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long) _
As Long
Private Sub CloseWindow()
Dim Handle As Long
Dim ie As InternetExplorer
Const NILL = 0&
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&
Set ie = UserDocument.Parent
' Send the message.
Handle = SendMessage(ie.hwnd, WM_SYSCOMMAND, SC_CLOSE, NILL)
End Sub
- Call the CloseWindow procedure whenever you want to close the Internet Explorer window.