DOCUMENT:Q182033 04-MAR-1998 [vbwin] TITLE :HOWTO: Use BeforeNavigate2 in VB to Get an Event from HTML PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0 OPER/SYS:WINDOWS KEYWORDS:vb432 VB4WIN vb5all vb5howto vbwin ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Control Creation and Enterprise Editions for Windows, version 5.0 - Microsoft Visual Basic Enterprise Edition, 32-bit only, for Windows, version 4.0 --------------------------------------------------------------------- SUMMARY ======= In an HTML page displayed in the WebBrowser control, you can trigger an event in the Visual Basic application by using the BeforeNavigate2 event in the WebBrowser control. For example, you can have a command button in the HTML page that closes the Visual Basic application when clicked. MORE INFORMATION ================ 1. Create a Standard EXE project from Visual Basic. Form1 is created by default. Add Microsoft Internet Controls to your Project Components. 2. Add a WebBrowser Control from to Form1. 3. Add code for Form1 Load event as follows: Private Sub Form_Load() WebBrowser1.Navigate "c:\mytest.htm" End Sub 4. Add code for WebBrowser1 BeforeNavigate2 event as follows: Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, _ URL As Variant, Flags As Variant, _ TargetFrameName As Variant, PostData As Variant, _ Headers As Variant, Cancel As Boolean) Dim Ev As String If UCase$(Left$(URL, 6)) = "EVENT:" Then 'this is our event, process it and cancel navigation Ev = UCase$(Mid$(URL, 7)) Select Case Ev Case "HELLO" MsgBox "Hello from VB App" 'set a field on HTML page WebBrowser1.Document.All("field1").Value = _ "Hello from VB" Case "CLOSE" If MsgBox("Are you sure you want to exit?", _ vbYesNo) = vbYes Then End End If End Select Cancel = True End If End Sub 5. From Notepad, type in the following text and save it as c:\mytest.htm or another location (be sure to use the same location in step 3):

6. Run your project from Visual Basic. ====================================================================== Keywords : vb432 VB4WIN vb5all vb5howto vbwin Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.