vba intercept internet explorer link click

ozmike

I'm using excel 2010, vba. I have a web page at work - which I created, and when the user clicks on a link, i want to intercept that link, and run vba (not the javascript onclick event). ie bind the onclick event to VBA function or sub if possible. Some sites hint it can be done. Question for an automation guru. Note i do not want to click the link thru automation (the below code does this) I want the user to click and the VBA intercept the click ( note, i don't want to replace the javascript with more js I want to call VBA). I'm using the InternetExplorer object but might need to use another ie object or library reference as you advise.

The below code (example) opens the www's first web page and clicks on the link. I want to intercept this click and run vb code.

Dim ie As InternetExplorer
Set ie = New InternetExplorer
sURL = "http://info.cern.ch/hypertext/WWW/TheProject.html" ' www's first web page

ie.Navigate sURL
ie.Visible = True
Do While ie.Busy
    DoEvents
Loop
Set oForm = ie.Document.getElementsByName("0") ' worlds first ever anchor/ hyper link
Set oLink = oForm.Item(0)

'oLink.onclick = ' set/add to VBA function to replace/set javascript onlclick event ie. to intercept click

oForm.Item(0).Click ' run vba code to display msgbox "hello World" not navigate
Tim Williams

You can use a class module and WithEvents to hook up VBA-hosted events which can be triggered from IE. This code is for links, but most other events can also be captured.

EDIT: added mouseover/out for good measure...

Regular module

Private lnks As Collection 'of clsLink

Sub Tester()

    Dim ie As InternetExplorer, el, sURL
    Dim lnk As clsLink

    Set ie = New InternetExplorer
    sURL = "http://info.cern.ch/hypertext/WWW/TheProject.html" 

    ie.Navigate sURL
    ie.Visible = True
    Do While ie.Busy
        DoEvents
    Loop

    Set lnks = New Collection

    For Each el In ie.document.getElementsByTagName("a")
        Set lnk = New clsLink
        lnk.Init el
        lnks.Add lnk
    Next

End Sub

clsLink (class module)

Option Explicit

'note "WithEvents" declaration
Private WithEvents lnk As MSHTML.HTMLAnchorElement

Private Function lnk_onclick() As Boolean
    Debug.Print "Link: '" & lnk.innerText & "' clicked!"
    lnk_onclick = False 'cancels navigation
    'lnk_onclick = True 'doesn't cancel navigation
End Function

Private Sub lnk_onmouseout()
    With lnk.Style
        .Color = "#00F"
        .backgroundColor = "#FFF"
    End With
End Sub

Private Sub lnk_onmouseover()
    With lnk.Style
        .Color = "#F00"
        .backgroundColor = "#0F0"
    End With
End Sub

Public Sub Init(el)
    Set lnk = el
End Sub

Add project references to:

  • Microsoft Internet Controls
  • Microsoft HTML Object Library

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

click on link text in internet explorer using vba

Click link in Internet Explorer

VBA Internet Explorer Webpage Link Selection

How to click a button in Internet Explorer using VBA

Excel VBA Internet explorer click button with no ID

link.click(); does not work in internet explorer while working in chrome

How to click the hyperlink (<a> tag) in Internet Explorer through Excel VBA?

iOS UIWebView intercept link click

vba internet explorer delete object

VBA - Addressing Internet Explorer tabs

Getting HTML link using Internet explorer with VBA excel (no code error but the results is wrong)

Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated

How to intercept local server web requests using Burp in Internet Explorer

Permission denied for clicking link in Internet Explorer 11

Internet Explorer 10 Printing URL Below Link

Selenium click not working on Internet Explorer 11

Problem with double click on checkbox with Internet Explorer

Automatically filling Internet Explorer inputbox - VBA in MSP

Change Internet Explorer encoding using VBA

Excel VBA Error 462 With Internet Explorer

VBA UI Automation - Internet Explorer "Save As"

VBA - terminating Internet Explorer processes via Shell

Toggle Internet Explorer Proxy from Excel VBA

VBA: set HTML as Internet Explorer object ERROR

VBA Select from dropdown in Internet Explorer

Control an Element in invisible Internet Explorer vba

VBA Select Item from List in Internet Explorer

VBA screen-scraping via Internet explorer

Link shortener target URL changed, Internet Explorer goes to old link