Capture Click and Double Click Events on WebBrowser Control (1 Viewer)

i2cmevans

New member
Local time
Today, 15:21
Joined
May 23, 2010
Messages
7
I have some WebBrowser controls on a form that basically are just image containers. I want to be able to initiate some events when a user either clicks or double-clicks on the WebBrowser control itself. Is this possible in VBA? I didn't have much luck searching around with Google.

Thanks.
 

MarkK

bit cruncher
Local time
Today, 15:21
Joined
Mar 17, 2004
Messages
8,186
The problem is that once your mouse is over the browser the mouse events are handled by the web page, not by Access or VBA.
What would probably work is to handle the DocumentComplete event of the web browser, which provides you with the web page as some kind of HTML DOM object which contains other objects that do source events you can handle in VBA.
Here's some code that might give you some ideas...
Code:
Private WithEvents m_body As MSHTML.HTMLBody
Private WithEvents m_ie As SHDocVw.WebBrowser

Private Sub Form_Open(Cancel As Integer)
   Set m_ie = Me.WebBrowser4.Object
   m_ie.Navigate2 "http://www.microsoft.com"
End Sub

Private Function m_body_onclick() As Boolean
   MsgBox "You clicked the page's body", vbInformation
End Function

Private Sub m_ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   Debug.Print URL
   If left(URL, Len("http://www.microsoft.com")) = "http://www.microsoft.com" Then
      Set m_body = pDisp.Document.body
   End If
End Sub
 

i2cmevans

New member
Local time
Today, 15:21
Joined
May 23, 2010
Messages
7
Thanks for the recommendations, I'm going to look into that this weekend.
 

CedarTree

Registered User.
Local time
Today, 18:21
Joined
Mar 2, 2018
Messages
404
I'm looking for similar help... but when I use the sample code, I get "user defined type not defined." Any suggestions?
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,350
You need to add the relevant references.?

Possibly Microsofr HTML Ojbect Library ?
Maybe also Microsoft WinHTTP services.

They might come up as missing?

HTH
 

CedarTree

Registered User.
Local time
Today, 18:21
Joined
Mar 2, 2018
Messages
404
Yeah I think it was a reference issue. But it also seems that the mouse events are lost when a PDF is open, since the PDF application takes over.
 

Users who are viewing this thread

Top Bottom