VBA fill webBrowser HTML inputbox

From Excel we can create can load an explorer instance (inside an userform for example). From there, one can reach any site, and more interesting, if there is a login filter, username and password can be fetch and go forward.

But there can be some caveats, for example GMail site that has a two stages login, so it has to be evaluated twice where to fill data between complete login.

Place a Microsoft Internet Controls (also Microsoft WebBrowser) -refered to ieframe.dll-, as is not in the default controls palette, you have to search for it in “Additional Controls”. For a site like GMail it Will complain a lot, as it old browser technology, but it’ll go to the end.

For the second stage, the function WebBrowser1_DocumentComplete, will analyse that first stage has been succesfully surpassed.

Add two textboxes and rename then to txtUserName and txtPassword, add a button named cmdAction. For instance, you can add a textbox and name it txtURL and finally a button cmdGo to navigate to desired site.

Paste the following code in the userform, and browse!

VBA_WebBrowser

Option Explicit

Private bWebChange As Boolean

'Private Sub txtURL_Change()
'End Sub

Private Sub txtURL_Enter()
Call cmdGo_Click
End Sub

Private Sub UserForm_Initialize()
'It's not the same to go from different webbrowser, site can change for each browser...
'Is is a multipage site?
With Me
.txtUsername.Text = ""
.txtPassword.Text = ""
End With
End Sub

Private Sub cmdGo_Click()
With Me
If .txtURL.Text Like "http*//*" Then
.txtURL.Text = .txtURL.Text
ElseIf .txtURL.Text Like "www*" Then
.txtURL.Text = "http://" & .txtURL.Text
Else
'count dot chars
.txtURL.Text = "http://www." & .txtURL.Text
End If
.WebBrowser1.Navigate (.txtURL.Text)
End With
End Sub

Private Sub cmdAction_Click()
' Always get "name" property
With Me
With .WebBrowser1
If Not bWebChange Then Exit Sub 'Wait for completion

' Go to site and search <input for input box... get "name" property
' Go to site and search "*button*" for buttons ...

.Document.all("Email").Value = Me.txtUsername.Text

bWebChange = False
.Document.all("signIn").Click

If bWebChange Then
'Wait until complete... go to next page
.Document.all("Passwd").Value = Me.txtPassword.Text
.Document.all("signIn").Click
End If
End With
End With
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If pDisp Is WebBrowser1.Application Then
Debug.Print "Complete"
bWebChange = True
MsgBox "Complete"
'
' Now you can poke around the Document Object
'
End If
End Sub
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *