+ Reply to Thread
Results 1 to 3 of 3

How to choose a select-list option using VBA macro ?

Hybrid View

Guest How to choose a select-list... 04-21-2006, 09:10 AM
Guest Re: How to choose a... 04-21-2006, 02:10 PM
Guest Re: How to choose a... 04-24-2006, 01:55 PM
  1. #1
    tmp2100
    Guest

    How to choose a select-list option using VBA macro ?

    I have an excel VBA macro to access a website, fill in some
    fields of a form, and then submit it. The macro works fine, but I am stuck on
    how
    to formulate a statement to choose a 'select-list' option:

    Sub XYZDataFetch()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate http://websitename_goes_here.com

    Do Until ie.ReadyState <> 1: DoEvents: Loop

    ' ******** here is where the statement goes to choose select-list option
    *******
    ie.document.all.Item("cstep").Click
    ie.document.all.Item("request").Value = "Summary Table"
    ie.document.forms(0).submit

    ' more code goes here
    End Sub

    The webpage I am accessing is written in javascript and contains
    the following select list object:

    <select name="tspan" onChange="ckCustomSpanVisible();">
    <option>All times</option>
    <option>Last 15 mins</option>
    <option>Last hour</option>
    <option selected="selected">Last 4 hours</option>
    <option id="custom">Custom</option>
    </select>

    QUESTION: at the indicated location in my macro VB code, what is the
    statement
    needed to force the select option away from the default 'Last 4 hours' and to
    select 'Custom' instead ?

    thanks,
    tmp2100

    --
    tmp2100

    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200604/1

  2. #2
    Tim Williams
    Guest

    Re: How to choose a select-list option using VBA macro ?

    Try this function.

    Tim

    '********************************

    Function SetSelect(s,val) as boolean
    dim x as integer
    dim r as boolean


    r=false
    for x=0 to s.options.length-1
    if s.options(x).text = val then
    s.selectedIndex=x
    r=true
    exit for
    end if
    next x


    SetSelect=r


    end function

    '*********************************
    usage:


    if not SetSelect(ie.document.all.Item("tspan"),"Custom") then
    'something went wrong
    else
    'continue...
    end if








    "tmp2100" <u21084@uwe> wrote in message news:5f1bd9db00456@uwe...
    >I have an excel VBA macro to access a website, fill in some
    > fields of a form, and then submit it. The macro works fine, but I am stuck on
    > how
    > to formulate a statement to choose a 'select-list' option:
    >
    > Sub XYZDataFetch()
    > Dim ie As Object
    >
    > Set ie = CreateObject("InternetExplorer.Application")
    > ie.Visible = True
    > ie.navigate http://websitename_goes_here.com
    >
    > Do Until ie.ReadyState <> 1: DoEvents: Loop
    >
    > ' ******** here is where the statement goes to choose select-list option
    > *******
    > ie.document.all.Item("cstep").Click
    > ie.document.all.Item("request").Value = "Summary Table"
    > ie.document.forms(0).submit
    >
    > ' more code goes here
    > End Sub
    >
    > The webpage I am accessing is written in javascript and contains
    > the following select list object:
    >
    > <select name="tspan" onChange="ckCustomSpanVisible();">
    > <option>All times</option>
    > <option>Last 15 mins</option>
    > <option>Last hour</option>
    > <option selected="selected">Last 4 hours</option>
    > <option id="custom">Custom</option>
    > </select>
    >
    > QUESTION: at the indicated location in my macro VB code, what is the
    > statement
    > needed to force the select option away from the default 'Last 4 hours' and to
    > select 'Custom' instead ?
    >
    > thanks,
    > tmp2100
    >
    > --
    > tmp2100
    >
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200604/1




  3. #3
    tmp2100 via OfficeKB.com
    Guest

    Re: How to choose a select-list option using VBA macro ?

    this works (for example):
    ie.document.all.Item("tspan").selectedIndex = 8
    ie.document.getElementById("customSpan2").Style.visibility = "visible"

    tmp2100


    =============================================================

    Tim Williams wrote:
    >Try this function.
    >
    >Tim
    >
    >'********************************
    >
    >Function SetSelect(s,val) as boolean
    > dim x as integer
    > dim r as boolean
    >
    > r=false
    > for x=0 to s.options.length-1
    > if s.options(x).text = val then
    > s.selectedIndex=x
    > r=true
    > exit for
    > end if
    > next x
    >
    > SetSelect=r
    >
    >end function
    >
    >'*********************************
    >usage:
    >
    >if not SetSelect(ie.document.all.Item("tspan"),"Custom") then
    > 'something went wrong
    >else
    > 'continue...
    >end if
    >
    >>I have an excel VBA macro to access a website, fill in some
    >> fields of a form, and then submit it. The macro works fine, but I am stuck on

    >[quoted text clipped - 37 lines]
    >> thanks,
    >> tmp2100


    --
    tmp2100

    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200604/1

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1