user1955215
user1955215

Reputation: 743

Internet Explorer Automation

I am trying to automate downloading of files from a website. Some code I have attempted so far is shown below. The page has four input fields (see url in the code below).

The page has one radio button where I need to select id "RadioChk_2", which has a label:"both". There are 3 select boxes and a button named "Export to Excel" The first select has displayed values e.g. Chandigarh(O4), which is stored as a value of 04. The code below attempts to fill in two select boxes but does not work. Please help or point to how to progress further.

Thanks in Advance

Sub WebFormSubmit()
Dim IE As InternetExplorer, URL As String, HTMLDoc As HTMLDocument
Set IE = New InternetExplorer
URL = "http://censusindia.gov.in/2011census/Listofvillagesandtowns.aspx"
IE.navigate URL
IE.Visible = True
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    DoEvents
    Loop
Set HTMLDoc = IE.document
HTMLDoc.getElementById("drpstate").Value = "Chandigarh(04)"
HTMLDoc.getElementById("drpstate").FireEvent "onchange"
HTMLDoc.getElementById("drpDistrict").Value = "All"

 'Wait for the response
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    DoEvents
    Loop
IE.Quit
End Sub

Upvotes: 0

Views: 338

Answers (2)

ron
ron

Reputation: 1378

I couldn't test completely because I don't know the correct 3- and 5-digit codes for input boxes two and three, but the following does click the desired radio button, interact with the input boxes and clicks the "Export to Excel" button. It should at least give you some direction for further work.

Sub WebFormSubmit()
Dim IE As InternetExplorer, URL As String, HTMLDoc As HTMLDocument

Set IE = New InternetExplorer

URL = "http://censusindia.gov.in/2011census/Listofvillagesandtowns.aspx"
IE.navigate URL
IE.Visible = True
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

HTMLDoc.getelementbyid("RadioChk_2").Checked = True
HTMLDoc.getelementbyid("drpState").Value = "04"
HTMLDoc.getelementbyid("drpDistrict").Value = "123"
HTMLDoc.getelementbyid("drpsubdistrict").Value = "12345"

HTMLDoc.getelementbyid("btnXls").Click

' Wait for the response
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

IE.Quit
End Sub

Upvotes: 0

CRondao
CRondao

Reputation: 1903

I tried like this and it worked

HTMLDoc.getelementbyid("drpstate").selectedindex = 6
HTMLDoc.getelementbyid("drpstate").FireEvent "onchange"
t = Timer
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE Or Timer - t < 1.5
 DoEvents
Loop
HTMLDoc.getelementbyid("drpDistrict").selectedindex = 1

t is a single

Upvotes: 0

Related Questions