You can scroll through all input tags and identify the corresponding one by checking the "most identifying" attribute (id, name, type, innerHTML, ... whatever). Here's the Sub () that I use in an Excel worksheet, which automatically registers on the website
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 the following parameters:
- Doc .... HTML DOM Object
- Tag .... tag you want to work on (usually input, image, a, ...)
- Attrib .... the attribute whose value you want to map
- Match .... matching attribute value
- Action ..... if the "/ C /" action .Click () is executed on the matching tag, otherwise the value of the action is placed in the value attribute of the tag
If you want to include any attribute containing JavaScript code (for example, "onclick"), do not forget that the code that you see in the HTML source is embedded in an anonymous function, for example
function anonymous() { onclick="exportData(workOrderSearchForm)"; }
You need to consider this with the Instr () function or similar if you want to connect, for example. "exportData (workOrder".
Also very important: give enough time to go to the page and for the DOM object to be loaded. I note in my company that after changing pages, loading times sometimes increase dramatically.
I have a good success:
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
I hope this inspires you to create your solution.
Good luck MikeD