Saturday, April 4, 2009

Webbrowser Control in VBA Excel / Word

Using the Webbrowser control is a helpful tool in many programming situations. One of the most common tasks that can be automated is interaction with websites. In the following example, I will setup the webbrowser control within the document itself, navigate to a website and send post data along to that site. I've changed the site location and the post data fields so the code will not function correctly until you enter that information in.

FYI: it is easy to find out the URL and the post data of the form you want to submit by following these steps:
Open your internet browser and view the site where the data would be entered. View the source code. You will find an HTML tag that says "form action=xxxxxx". The URL we want will be the "xxxxx" portion in that line. Then you will want to note all of the input tag name values within that form. These will look like "input type=xxx name=xxx". When posting your data (or when using a GET command, for that matter) you will need to know the input names so you can send the relevant data along with them.
In the example below i've included inputs named "fname", "lname", and "zip" which you will see in the postdata variable. The script below also reads through a list of 10 rows in an excel spreadsheet. If column "A" has data in it, then it pulls data from Columns A, B, and C corresponding to our variables "fname", "lname", and "zip", in that order. Then it moves on to the next row and loops until there is no more data or until row 10 is reached.


Sub getpage()
Dim URL As String, PostData() As Byte, kp as integer
URL = "https://examplepage.com/script.cgi"
For kp = 1 To 10
'iterate through the first ten rows
If Not Me.Cells(kp, 1) = "" Then
'if cell A1 isn't empty then proceed
PostData = "fname=" & Me.Cells(kp, 1) & "&lname=" _
& Me.Cells(kp, 2) & "&zip=" & Me.Cells(kp, 3)
'set our postdata string to the variables and their
'corresponding values
PostData = StrConv(PostData, vbFromUnicode)
'convert the string
Me.WebBrowser1.Silent = True
'stop the browser from alerting us of anything
Me.WebBrowser1.Navigate URL, 0, "", PostData, ""
'tell the browser to goto the URL
Do
DoEvents
Loop Until Me.WebBrowser1.ReadyState = READYSTATE_COMPLETE
'let this loop run until the webbrowser has finished
'loading the page
End If
Next kp
End Sub


As you will see, the code can iterate through a list of items and run a request to a website, filling in the data the user desires and retrieving the results. the me.webbrowser1.silent = true code stops the browser from spitting any messageboxes back at us so we can be sure there are no problems automating the process. The line "loop until me.webbrowser1.readystate = readystate_complete" is a line that waits until the webbrowser control has responded that it has fully finished loading the page. the words readystate_complete can be replaced by the number 4, as they are interchangeable. However, I have found that this code is often flawed as the webbrowser does not always return a reliable result about whether it has finished loading (it often says it's done loading but has not loaded the innerhtml property yet). I will deal with this issue in a later post.

For now, this should be a sufficient example to get you POSTing data using the webbrowser control. A GET command would be even simpler. You just append the data that is in the post variable to the end of the web address, with a question mark separating the web address and the postdata (ie. http://testsite.com/script.cgi?variable1=value&variable2=2ndvalue&variable3=thirdvalue)
Note, when combining the variables in the string you separate each new variable with the "&" character and place the input name on the left of the equals sign and the value on the right of it.

No comments:

Post a Comment