I want to know how to how to fillup a web form programmatically using VB.
For example I want to fill data in the following page (image attached) and click the 'go' buttom programmatically. Then I want to save the URL of the resulting page to a string variable.
You can use the Internet Explorer Object (DOM If i am correct).
example : Filling GoOogle search box and click the search button.
Code:
Private Sub Command1_Click()
Dim oIE As Object
Dim URL As String
Set oIE = CreateObject("InternetExplorer.Application")
URL = "http://www.google.com"
oIE.Visible = True
oIE.navigate (URL)
You need to find the name of the textbox & the name fo the "Submit" button of your webpage.
I recommed you to use "IE Developer Toolbar" to find hease items on the webapge.
Do While oIE.readyState <> 4 And oIE.Busy
DoEvents
Loop
oIE.Document.All.Item("q").Value = "Football"
oIE.Document.All.Item("btnG").Click
End Sub
You need to find the name of the Textbox & the name of the Submit button of the webpage shown in your picture. If you have installed "IE Developer Toolbar" it is very easy to find eliments on the webpages.
oh, this will going to have some serious privacy issues.
when i click that link, it asks for a code "Enter the first few characters of the company's name or the NSE symbol or BSE code and click ' Go ' "
* Please don't post your personal account informations in the forum !!!!
Did you check that text box name and the button name with "IE Developer Toolbar" ?
first please make sure the names are correct. you also should try Doogles suggession.
My earlier suggestion works for me. However, I noticed that
Code:
Do While oIE.ReadyState <> 4 And oIE.Busy
DoEvents
Loop
was unreliable as a method of determining whether the Document had completely loaded. In some instances it worked ok and in others I received the traditional "Object variable with block variable not set" error message, meaning that the document wasn't ready. I'm not sure if there's a better method when you're creating an instance of Explorer as you are. Perhaps others will know.
A suggestion might be to use the WebBrowser control where you can use various events - in particular the DocumentComplete event which is triggered when a URL is loaded.
eg
Private Sub wb1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
End Sub
Since this event is passed the URL being completed you can compare that to the one requested and if there's a match you know that it's ready.
Even with that method you have to be very careful. For example, navigating to http://www.vbforums.com causes 2 http requests to be made and therefore there will be 2 DocumentComplete events triggered, but the URL returned in the DocumentComplete event has a trailing "/" added. Also the order the requests are completed may be different from the order they were made.
The code below demonstrates:
Code:
Private strURL As String
Private boLoaded As Boolean
Private Sub Command1_Click()
Dim intI As Integer
strURL = "http://www.vbforums.com/"
boLoaded = False
wb1.Navigate strURL
Do Until boLoaded = True
intI = intI + 1
If intI = 1000 Then
DoEvents
intI = 1
End If
Loop
'
' We can be reasonably sure that the document is now loaded
'
End Sub
Private Sub wb1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Static intCount As Integer
intCount = intCount + 1
Debug.Print "DocumentComplete " & intCount & " " & URL
If URL = strURL Then boLoaded = True
End Sub
Private Sub wb1_DownloadBegin()
Static intCount As Integer
intCount = intCount + 1
Debug.Print "DownLoadBegin " & intCount
End Sub
Private Sub wb1_DownloadComplete()
Static intCount As Integer
intCount = intCount + 1
Debug.Print "DownLoadComplete " & intCount
End Sub
Private Sub wb1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
Static intCount As Integer
intCount = intCount + 1
Debug.Print "NavigateComplete " & intCount & " " & URL
End Sub
I've coded up some of the other navigation related events just to indicate the order of things happening.
If you run this, you'll see that even though the forum's main page was requested first, it doesn't complete until the second DocumentComplete event. So if you don't check which URL has completed and take action on the first DocumentComplete event you run the risk of an error being generated when you try to access the Document. (But, experience shows, it is timing dependent so it wont fail all of te time!!)
One other little "feature" of the WebBrowser control you have to watch out for is, that if you set it's Visible property = False, the DocumentComplete event never triggers ! I think this control was written on a Friday afternoon after a serious session at the local pub
Last edited by Doogle; Jan 15th, 2008 at 02:11 AM.