DonGarry
asked on
How to use the ActiveX Microsoft WebBrowser Control
I've got the follow code working just fine as long as I'm just filling in one control on the webpage. My question is how would I revise the code below to fill in multiple web input controls plus how would I programmatically fire an on_click event using VBA for Access?
Sub Main()
Dim WebBrowser1 As HTMLObjectElement
'what is the page you want to pull up in your browser (ie icq.com/sms)
WebBrowser1.navigate "http://www.google.com"
'sometimes there are timing issues, so issue a couple of doevents so the browser responds
DoEvents: DoEvents: DoEvents
Do While WebBrowser1.Busy 'wait until the page loads by looping
DoEvents
Loop
Set doc = WebBrowser1.Document 'access the document properties of the current page
DoEvents
Do While WebBrowser1.Busy
DoEvents
Loop
'if you pull your page up in IE and you view the source,
'you can find the form variables that you want to fill in
'I'm sure there are other ways to iterate through the form
' to get them, but I never could figure it out.
returnValue = FillForm("q", "this is my search criteria", False)
WebBrowser1.Document.Forms(0).submit
End Sub
Public Sub ClickLink(doc, LinkText As String)
For i = 0 To doc.links.length - 1
If InStr(LTrim(RTrim(doc.links(i).outerText)), LinkText) > 0 Then
doc.links(i).Click
Exit For
End If
Next i
End Sub
Function FillForm(ByVal formtag, ByVal FillValue, ByVal isCombo As Boolean) As Boolean
Dim elemcollection As IHTMLElementCollection
Dim obj As Object
Dim element2 As HTMLInputElement
Dim element As HTMLInputElement
If Not isCombo Then
WebBrowser1.Document.Forms(0).elements(formtag).Value = FillValue
Else
WebBrowser1.Document.Forms(0).elements(formtag).selectedIndex = Val(FillValue)
End If
End Function
ASKER
You are correct, I have a WebBrowser1 Control and the exisitng code fills in ONLY one input box called 'q'. My question is how would I change our code to fill in two more input boxes say 'username' & 'password' and then click the 'submit' button in the WebBrowser1 Control?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Function FillForm(ByVal formtag, ByVal FillValue, ByVal isCombo As Boolean) As Boolean
Dim elemcollection As IHTMLElementCollection
Dim obj As Object
Dim element2 As HTMLInputElement
Dim element As HTMLInputElement
If Not isCombo Then
WebBrowser1.Document.Forms
Else
WebBrowser1.Document.Forms
End If
End Function
If you wanted to fill another document in a different webrowser control, you'd just reference that control in your If Not isCombo block:
WebBrowser2.Document.Forms
But I suspect there's more to it than this ... can you explain a bit more as to what you want to do?