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
J PAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

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

Open in new window

Regards
0
J PAuthor Commented:
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?
0
Rgonzo1971Commented:
then try
doc.GetelementsByName("ad_award_name")(0).value = "Test1" 
doc.GetelementsByName("ad_award_name")(0).FireEvent("onchange")

Open in new window

0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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

Any idea?
0
Rgonzo1971Commented:
Nope
0
J PAuthor Commented:
Thanks for trying Rgonzo, much appreciated.
0
J PAuthor Commented:
I am running IE11. I wonder if the issue is with the fireevent line because of IE11?
0
Rgonzo1971Commented:
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

0
J PAuthor Commented:
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

0
Rgonzo1971Commented:
then try

set e = ie.document.getElementsByName("ad_award_name")(0)
    For Each o In e.Options
        If StrComp(o.Text, "Test1", vbTextCompare) = 0 Then
            o.Selected = True
            Exit For
        End If
    Next
e.FireEvent ("onchange")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J PAuthor Commented:
Brilliant! Thank you! Works perfectly!

How did you solve it?
0
Rgonzo1971Commented:
So instead of giving the value I loop trough the values and select the one corresponding to your text
1
J PAuthor Commented:
Thanks Rgonzo! Have a good day!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.