PDA

View Full Version : VBA code to fill in a combo-box of an IE site linked from another IE site



ChrisZim
08-09-2015, 11:12 PM
Hi Everyone

I have the code below, which navigates to a website, clicks on a link on that site to open another page, and then attempts to fill in the first dropdown list (or combobox) with the value 2012. It all works except for the last part of filling in the dropdown list. If someone could let me know how to modify that code that would be great. I just get a run-time error '462': The remote server machine does not exist or is not available.

Sub GetQuote()
Dim IE AsObject
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("https://wwwDOTaamiDOTcomDOTau")
IE.Visible =True
Do
DoEvents
LoopUntil IE.readystate =4
Dim e
Set e = IE.document.GetElementsByClassName("sg-Btn sg-Btn--primary")(1)
e.Click
Do
DoEvents
LoopUntil IE.readystate =4

Dim a As Object

Set a = IE.document.getElementbyid("vehicleYearOfManufactureList")
a.SelectedIndex =4

EndSub

ChrisZim
08-10-2015, 10:58 PM
Someone helped me with this - there needed to be some kind of delay after the new page was launched.

Inserting the following worked:

Application.Wait (Now + TimeValue("00:00:02"))

The only problem now is that I can't seem to fill in the second dropdown list which depends on the value of the first one. Even though the first dropdown list populated it looks as though the page thinks that I have not actually selected anything. Once again I'm at my wits end. Any help would be greatly appreciated as always.

Here is the code I have now. If someone could run this and let me know they think that would be really great - thanks:

Sub GetQuote()


Dim IE As Object


Set IE = CreateObject("InternetExplorer.Application")


IE.navigate ("https://www<dot>aami<dot>com<dot>au")


IE.Visible = True


Do
DoEvents
Loop Until IE.readystate = 4


Dim e
Set e = IE.document.getElementsByClassname("sg-Btn sg-Btn--primary")(1)
e.Click


Application.Wait (Now + TimeValue("00:00:02"))


Do
DoEvents
Loop Until IE.readystate = 4


Dim z As Object


Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")
z.SelectedIndex = 4


z.fireEvent ("OnChange")




Set y = IE.document.getElementbyid("vehicleMakeList")
y.Value = "Toyota"




'I've also tried: y.SelectedIndex = 2... just to see if that would select a make in the make dropdown list but it doesn't work either








End Sub

ChrisZim
08-10-2015, 11:37 PM
In fact, on executing the code I noticed that when I hovered over the second dropdown list which is blank, instead of there being the normal arrow representing my mouse cursor, there is a little white hand with a black circle that has a strike through it (as if to suggest that the dropdown cannot be selected. And that is indeed the case, i.e. that none of the dropdowns can be manually selected except for the first one). Please, please help, someone!

mancubus
08-10-2015, 11:42 PM
welcome to VBAX.

please use code tags when posting your code. # button inserts code tags.

try to adopt below code, from http://www.xtremevbtalk.com/showthread.php?t=325370, which seems a solution to similar requests.



Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_MAXIMIZE = 3
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const READYSTATE_COMPLETE = 4

Sub LoadPage()
Dim IE As Object
Dim lst As Object
Dim evt As Object

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
apiShowWindow IE.hwnd, SW_SHOWNORMAL
Sleep lng_Sleep
IE.Navigate "https://www.avanza.se/aktier/lista"

Sleep 5000

Do
DoEvents
Sleep 500
Loop While IE.readyState <> READYSTATE_COMPLETE

Set evt = IE.document.createEvent("HTMLEvents")

evt.initEvent "change", True, False

Set lst = IE.document.getElementById("list")

lst.selectedIndex = 1

lst.dispatchEvent evt

End Sub

ChrisZim
08-11-2015, 08:07 PM
Thanks for the reply, Mancubus, and for directing me to the thread. Unfortunately this doesn't seem to be working for me. I don't know if it's because I'm using IE11? Here is the code I ended up with:


Sub GetQuote()

Dim IE As Object


Set IE = CreateObject("InternetExplorer.Application")


IE.navigate ("https://www<dot>aami<dot>com<dot>au")


IE.Visible = True


Do
DoEvents
Loop Until IE.readystate = 4


Dim e
Set e = IE.document.getElementsByClassname("sg-Btn sg-Btn--primary")(1)
e.Click


Application.Wait (Now + TimeValue("00:00:02"))


Do
DoEvents
Loop Until IE.readystate = 4




Dim z As Object


Set evt = IE.document.createEvent("HTMLEvents")

evt.initEvent "change", True, False

Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")


z.SelectedIndex = 4
z.dispatchEvent evt


Application.Wait (Now + TimeValue("00:00:02"))


Dim y As Object


Set evt = IE.document.createEvent("HTMLEvents")

evt.initEvent "change", True, False


Set y = IE.document.getElementbyid("vehicleMakeList")


y.Value = "TOYOTA"
y.dispatchEvent evt


End Sub

And I also tried the following:


Sub GetQuote()

Dim IE As Object


Set IE = CreateObject("InternetExplorer.Application")


IE.navigate ("https://www<dot>aami<dot>com<dot>au")


IE.Visible = True


Do
DoEvents
Loop Until IE.readystate = 4


Dim e
Set e = IE.document.getElementsByClassname("sg-Btn sg-Btn--primary")(1)
e.Click


Application.Wait (Now + TimeValue("00:00:02"))


Do
DoEvents
Loop Until IE.readystate = 4




Dim z As Object
Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")
z.Focus
z.SelectedIndex = 4
z.FireEvent ("onchange")


Application.Wait (Now + TimeValue("00:00:02"))


Dim y As Object
Set y = IE.document.getElementbyid("vehicleMakeList")
y.Focus
y.Value = "TOYOTA"
y.FireEvent ("onchange")




End Sub

And here is the source code for the second dropdown:


<select id="vehicleMakeList" name="vehicleSelection.vehicleMake" class="sg-Input sg-u-inlineBlock size-medium user-success">
<option value="">Select</option><optgroup label="Common Makes" id="commonVehicleMakeOptionGroup"><option value="AUDI">Audi</option><option value="BMW">BMW</option><option value="FORD">Ford</option><option value="HOLDEN">Holden</option><option value="HONDA">Honda</option><option value="HYUNDAI">Hyundai</option><option value="KIA">Kia</option><option value="MAZDA">Mazda</option><option value="MERCEDES-BENZ">Mercedes-Benz</option><option value="MITSUBISHI">Mitsubishi</option><option value="NISSAN">Nissan</option><option value="SUBARU">Subaru</option><option value="TOYOTA">Toyota</option><option value="VOLKSWAGEN">Volkswagen</option><option disabled="disabled"></option></optgroup><optgroup label="All Makes" id="allVehicleMakeOptionGroup"><option value="ABARTH">Abarth</option><option value="ALFA ROMEO">Alfa Romeo</option><option value="ASTON MARTIN">Aston Martin</option><option value="AUDI">Audi</option><option value="BENTLEY">Bentley</option><option value="BMW">BMW</option><option value="CHERY">Chery</option><option value="CHRYSLER">Chrysler</option><option value="CITROEN">Citroen</option><option value="DODGE">Dodge</option><option value="FIAT">Fiat</option><option value="FORD">Ford</option><option value="FOTON">Foton</option><option value="GEELY">Geely</option><option value="GREAT WALL MOTORS">Great Wall Motors</option><option value="HOLDEN">Holden</option><option value="HONDA">Honda</option><option value="HSV">HSV</option><option value="HYUNDAI">Hyundai</option><option value="INFINITI">Infiniti</option><option value="ISUZU">Isuzu</option><option value="IVECO">Iveco</option><option value="JAGUAR">Jaguar</option><option value="JEEP">Jeep</option><option value="KIA">Kia</option><option value="LAND ROVER">Land Rover</option><option value="LEXUS">Lexus</option><option value="LOTUS">Lotus</option><option value="MAHINDRA">Mahindra</option><option value="MAZDA">Mazda</option><option value="MERCEDES-BENZ">Mercedes-Benz</option><option value="MINI">Mini</option><option value="MITSUBISHI">Mitsubishi</option><option value="NISSAN">Nissan</option><option value="OPEL">Opel</option><option value="PEUGEOT">Peugeot</option><option value="PORSCHE">Porsche</option><option value="PROTON">Proton</option><option value="RANGE ROVER">Range Rover</option><option value="RENAULT">Renault</option><option value="SAAB">Saab</option><option value="SKODA">Skoda</option><option value="SMART">Smart</option><option value="SSANGYONG">Ssangyong</option><option value="SUBARU">Subaru</option><option value="SUZUKI">Suzuki</option><option value="TATA">Tata</option><option value="TOYOTA">Toyota</option><option value="VOLKSWAGEN">Volkswagen</option><option value="VOLVO">Volvo</option></optgroup></select>

As always I'd be so grateful if someone could take a look and possibly run my code to advise me where things are going wrong etc. Please note that I have had to replace periods in links with "<dot>"s - Thanks

mancubus
08-11-2015, 11:26 PM
you are welcome.

searching via "excel vba ie 11 fireevent" key words looks promising.