If I understand your question correctly, you are trying to respond in VBA to the user clicking an anchor? If so you need to create an EventHandler for OnClick event. To do this you must restructure your code to meet the following criteria:
- You must declare said Anchor variable using the "WithEvents" keyword in order to respond to events.
- To use the "WithEvents" keyword you must declare your Anchor variable at the module level.
- You must also be inside a Class module to use the WithEvents keyword.
- Finally you need to create the Event Handler itself via the drop down menus in the top of the VBE or just paste in the code below.
How to use the code:
Create a Class Module named "MyClass" and paste in the following:
Option Explicit
Private WithEvents ieAnchor As MSHTML.HTMLAnchorElement
Public Sub Main()
Dim iePage As MSHTML.HTMLDocument
Set iePage = ieApp.document
Set ieAnchor = iePage.getElementsByTagName("A").Item(5)
End Sub
Private Function mobjMyAnchor_onclick() As Boolean
''//Your Code Here
End Function
To use the code in the class you can then add a standard module and call the code like this:
Public Sub Example()
Dim objMyClassVariable As MyClass
Set objMyClassVariable = New MyClass
objMyClassVariable.Main
End Sub
Edit:
To call a JavaScript function you can just use the JavaScript URL scheme with the InternetExplorer.Navigate method. Ex: ie.Navigate "JavaScript:SomeFunc()"
. Or you can actually just cause the click to occur using the HTMLAnchorElement.Click method: ieAnchor.Click
.