I\'m trying to get my VBA routine to interact with a page that uses JavaScript, and have been successful in the past. After a recent update, it no longer works. At this po
Try this:
Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = ie.Document.parentWindow
Call CurrentWindow.execScript("exportData(workOrderSearchForm)")
But first you will need to include Microsoft HTML Object Library under References (Tools>References)
Like this:
ie.window.exportData(ie.document.forms.workOrderSearchForm)
You can cycle thru all input tags and identify the relevant one by inspecting "the most identifying" attribute (id, name, type, innerHTML, ...whatever). Here's a Sub() I use in an Excel sheet which automatically logs on to a web site
Sub FormAction(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Attrib As String, ByVal Match As String, ByVal Action As String)
Dim ECol As MSHTML.IHTMLElementCollection
Dim IFld As MSHTML.IHTMLElement
Dim Tmp As String
Set ECol = Doc.getElementsByTagName(Tag)
For Each IFld In ECol ' cycle thru all <[tag]> elements
If VarType(IFld.getAttribute(Attrib)) <> vbNull Then ' does it contain the attribute
If Left(IFld.getAttribute(Attrib), Len(Match)) = Match Then
If Action = "/C/" Then
IFld.Click
Else
IFld.setAttribute "value", Action
End If
Exit Sub
End If
End If
Next
End Sub
The function takes following parameters:
If you want to hook on any attribute containing JavaScript code (like "onclick") don't forget that the code you see in the HTML source is embeded in an anonymous function, like
function anonymous()
{
onclick="exportData(workOrderSearchForm)";
}
You need to consider this by using an Instr() function or similar, if you want to hook on e.g. "exportData(workOrder" .
Also very important: give enough time for the page to navigate to and for the DOM object to be loaded. I notice in my company that once pages are changed the loading times sometimes raise drastically.
I have good success by this:
Sub MyMainSub()
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
' my other Dim's
' start browser
Set Browser = New SHDocVw.InternetExplorer
Browser.navigate "http://www.whatever.com"
WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec
' gain control over DOM object
Set HTMLDoc = Browser.document
WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec
' do actions
' FormAction HTMLDoc, w, x, y, z
End Sub
Sub WaitForBrowser(Browser As SHDocVw.InternetExplorer, Optional TimeOut As Single = 10)
Dim MyTime As Single
MyTime = Timer ' seconds since midnight
Do While Browser.Busy Or (Timer <= MyTime + TimeOut)
DoEvents ' go do something else
Loop
If Browser.Busy Then
MsgBox "I waited for " & Timer - MyTime & " seconds, but browser still busy" & vbCrLf & _
"exititing Login sequence now"
End
End If
End Sub
Hope this is inspiring enough for you to create your solution.
Good luck MikeD