Link to home
Start Free TrialLog in
Avatar of DonGarry
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

Open in new window

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Your function below seems to be the one that fills in your values. It specifically references a control named "WebBrowser1", which is your web broswer control (at least I'd hope it is <g>).

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

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 ect ect

But I suspect there's more to it than this ... can you explain a bit more as to what you want to do?
Avatar of DonGarry
DonGarry

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
Avatar of DonGarry
DonGarry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial