Skip to content Skip to sidebar Skip to footer

VBScript To Click Link On A Web Page Table That Calls Embedded (?) Javascript Function

At my work we have a very tedious process where we have to pull information from a web database and enter into a word document. Right now, we have no way to query the database and

Solution 1:

Thanks to everyone who helped out. However, for some reason, the above methods didn't work out in this particular webpage. I did, however, find a solution and wanted to post the answer in case it helps anyone else who may be stuck with this issue.

The code to click the requested link is below.

Option Explicit

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Sub Routine()

Dim IE as Object
Set IE = GetIE

With IE

    .Visible = True
    .Navigate "https://myURL/Default.aspx"

    IEWait

    Dim oDoc as Object, sLink as String

    Set oDoc = .document
    sLInk = "in_1191_1"

    IECLickLink oDoc, sLink

End Sub

Sub IEClickLink(doc As Object, sLink As String)
'assumes IE is loaded as InternetExplorer.Application") with URL loaded
'assumes doc is declared as IE.document

With doc

    Dim oLink As Object
    For Each oLink In .Links

        If InStr(1, oLink.href, sLink) Then 'looks for particular text in href attribute then clicks that particular link
            oLink.Click
            Exit For
        End If
    Next

End With

End Sub

Function GetIE() As Object
  On Error Resume Next
  Set GetIE = CreateObject("InternetExplorer.Application")
End Function

Sub IEWait()
'assumes IE is loaded as InternetExplorer.Application

With IE
    Do While .Busy Or .ReadyState <> 4: Sleep (100): Loop
End With

End Sub

Solution 2:

You may also have been able to use a CSS selector of

a[href=javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm')]

This literally says get the a tag element with href attribute whose value is javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm'

It would have been applied as:

oDoc.querySelector("a[href=javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm')]").Click

Solution 3:

You can always use the execScript() function to call a JavaScript function:

IE.document.parentWindow.execScript "ms('[enter]','HATSForm');", "javascript"

Post a Comment for "VBScript To Click Link On A Web Page Table That Calls Embedded (?) Javascript Function"