capture webbrowser navigation events (not a question, but solution)

  • Thread starter Eugene via AccessMonster.com
  • Start date
E

Eugene via AccessMonster.com

Here's an approach that I have developed to capture navigation events from
the webbrowser control.
If someone can provide a more elegant solution, please share!


1) Create a blank MS Access database

2) Create a form

3) Create a WebBrowser control on the form

4) Put the following text into the form's code:

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
Me.WB.Width = Me.WindowWidth
Me.WB.Height = Me.WindowHeight
MeWB.Navigate Application.CurrentProject.Path & "\webif.htm"
End Sub

Private Sub WB_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags
As Variant, TargetFrameName As Variant, PostData As Variant, Headers As
Variant, Cancel As Boolean)
If InStr(URL, "#") > 0 Then
Select Case UCase(Mid(URL, InStr(URL, "#") + 1))
Case "RELOAD"
Me.WB.Navigate Application.CurrentProject.Path & "\webif.htm"
Case "MSGBOX"
MsgBox "Hello, World!"
Case "INPUTBOX"
q = InputBox("What is your name?", "Question", "")
Case "CLOSEDB"
DoCmd.Quit acQuitSaveAll
End Select
End If
End Sub

5) Save the form

6) Copy the code below into a blank text file and save as "webif.htm". Place
the file in the same folder as the database you have created.

7) Open the form and test all links.
________________________________________________________________________________

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-
1252">
<meta http-equiv="Content-Language" content="en-us">
<title>
WebBrowser-to-VBA interface sample
</title></head>

<body topmargin="2" leftmargin="2" rightmargin="2" bottommargin="2"
marginwidth="0" marginheight="0">

<a name="CloseDB"></a>
<a name="MsgBox"></a>
<a name="Reload"></a>
<a name="InputBox"></a>

<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr><td colspan="2">
<font color="#006000"><b>WebBrowser-to-VBA interface
sample</b></font></td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
This sample provides an approach with which VBA can control
events happening on
the WebBrowser control places on a form in MS Access (also
applicable to MS
Excel).</td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
Everything is being done with the links in HTML document loaded
into WebBrowser
control. In WebBrowser control, WB_BeforeNavigate2 and
WB_NavigateComplete2
events are used.</td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
Sample links:</td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
1) <a href="#CloseDB">Close the database</a></td></tr>
<tr><td colspan="2">
2) <a href="#MsgBox">Show a message box</a></td></tr>
<tr><td colspan="2">
3) <a href="#Reload">Reload this page</a></td></tr>
<tr><td colspan="2">
4) <a href="#InputBox">Ask for a value</a></td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
Steps to accomplish this demo:</td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
1) Create a blank MS Access database<br>
&nbsp;</td></tr>
<tr><td colspan="2">
2) Create a form<br>
&nbsp;</td></tr>
<tr><td colspan="2">
3) Create a WebBrowser control on the form<br>
&nbsp;</td></tr>
<tr><td colspan="2">
4) Open VBA (Alt+F11) and place the following text into the
form's code:<br>
&nbsp;</td></tr>
<tr><td width=20>
&nbsp;</td><td>
<font face="Courier New">Private Sub Form_Open(Cancel As Integer)
<br>
&nbsp;&nbsp; DoCmd.Maximize<br>
&nbsp;&nbsp; Me.WB.Width = Me.WindowWidth<br>
&nbsp;&nbsp; Me.WB.Height = Me.WindowHeight<br>
&nbsp;&nbsp; MeWB.Navigate Application.CurrentProject.Path &amp;
&quot;\webif.htm&quot;<br>
End Sub<br>
<br>
Private Sub WB_BeforeNavigate2(ByVal pDisp As Object, URL As
Variant, Flags As
Variant, TargetFrameName As Variant, PostData As Variant,
Headers As Variant,
Cancel As Boolean)<br>
&nbsp;&nbsp; Me.Caption = URL<br>
&nbsp;&nbsp; If InStr(URL, &quot;#&quot;) &gt; 0 Then<br>
&nbsp;&nbsp;nbsp;&nbsp;&nbsp; Select Case UCase(Mid(URL, InStr
(URL, &quot;#&quot;) + 1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case
&quot;RELOAD&quot;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Me.WB.Navigate Application.CurrentProject.Path &amp; &quot;\
webif.htm&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case
&quot;MSGBOX&quot;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox
&quot;Hello, World!&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case
&quot;INPUTBOX&quot;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q =
InputBox(&quot;What is your name?&quot;, &quot;Question&quot;,
&quot;&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case
&quot;CLOSEDB&quot;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DoCmd.Quit

acQuitSaveAll<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Select<br>
&nbsp;&nbsp; End If<br>
End Sub</font></td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
<tr><td colspan="2">
5) Save the form and open it</td></tr>
<tr><td colspan="2">
&nbsp;</td></tr>
</table>
</body>
</html>
 
S

Stefan Hoffmann

hi Eugene,
Here's an approach that I have developed to capture navigation events from
the webbrowser control.
If someone can provide a more elegant solution, please share!
Nope. That is the correct way to do it.


mfG
--> stefan <--
 

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