Link to home
Start Free TrialLog in
Avatar of J P
J P

asked on

Select Value from Webpage Dropdown using VBA

I wish to use vba to select a value from a webpage dropdown box.

The name of the dropdown box is "ad_award_name" so I need to use (presumably) GetelementsByName("ad_award_name").

<select name="ad_award_name" onchange="javascript:submitLink(document.Form0,'NotRequired');" displayname="Award Name">
</select>

Open in new window


I want to select from the dropdown box the value "Test1".

How would I do this please?

Thanks

Jim
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
doc.GetelementsByName("ad_award_name").value = "Test1"
doc.GetelementsByName("ad_award_name").FireEvent("onchange")

Open in new window

Regards
Avatar of J P

ASKER

1:doc.GetelementsByName("ad_award_name").value = "Test1" did not work.

I got the VBA error "Run-Time error '438', Object doesn't support this property or method".

Any ideas?
then try
doc.GetelementsByName("ad_award_name")(0).value = "Test1" 
doc.GetelementsByName("ad_award_name")(0).FireEvent("onchange")

Open in new window

Avatar of J P

ASKER

No error message on 1: or 2: BUT nothing has changed on webpage. That is "Test1" has not been selected.

Any idea?
Avatar of J P

ASKER

Thanks for trying Rgonzo, much appreciated.
Avatar of J P

ASKER

I am running IE11. I wonder if the issue is with the fireevent line because of IE11?
then try
 Dim objEvent
    doc.GetelementsByName("ad_award_name")(0).value = "Test1"
    Set objEvent = doc.createEvent("HTMLEvents")
    objEvent.initEvent "change", False, True
    doc.GetelementsByName("ad_award_name")(0).dispatchEvent objEvent

Open in new window

Avatar of J P

ASKER

Ok the below works BUT I want to I want line 2: to = "Test1" rather than the selectindex. Any idea?

ie.document.getElementsByName("ad_award_name")(0).Focus
ie.document.getElementsByName("ad_award_name")(0).selectedIndex = 2 
ie.document.getElementsByName("ad_amount_payable")(0).Focus
ie.document.getElementsByName("ad_award_name")(0).FireEvent ("onchange")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of J P

ASKER

Brilliant! Thank you! Works perfectly!

How did you solve it?
So instead of giving the value I loop trough the values and select the one corresponding to your text
Avatar of J P

ASKER

Thanks Rgonzo! Have a good day!