Using the Selected Index

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Using the Selected Index

Post by Joseph »

I am currently trying to manipulate a drop down box on a web site. I can change the values, however they do not seem to actually take. I am currently using this code:

Code: Select all

      .document.getElementById("ctl00_cphMain_ddlLocation").Focus
    .document.all("ctl00_cphMain_ddlLocation").Value = "84"
The object ("ctl00_cphMain_ddlLocation") is a drop down box. Rather than iserting the value "84", I think I need to use the 'selected index'. I just do not know how. Could someone please give me an example.

Thanks
Joseph

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

Does this work?

.document.all("ctl00_cphMain_ddlLocation").Items.FindByValue("84").Selected = True
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

Getting a run time error '438' object does not support this property or method...

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

Just to make sure - are you using JavaScript, VBScript, or ...?
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

I am using VB sorry, trying to export a web report in to Excel.

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

Thanks. How about

.document.all("ctl00_cphMain_ddlLocation").Items.FindByText("84").Selected = True
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

Hmmm...still the same error. The issue is, this is the first object of 5, and if it is not selected properly, the other 4 objects will not load.

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

All my Googling points to using FindByValue or FindByText to return the item and then setting Selected = True. Are you really really sure that ctl00_cphMain_ddlLocation is the dropdown you want?
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

I'm positive. If I use this:

.document.getElementById("ctl00_cphMain_ddlLocation").Focus

The box is highlighted, that is how I check all the id's from the source.

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

Ok, that's convincing. But I have no idea how to solve it, sorry.
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

Fair enough Hans, thanks.

I'm thinking I need to get a little more creative. Is it possible to select the object and use a key stroke like up arrow? If so, would you have a code example for using keystokes? When manually using the page the 'up arrow' fires the event I'm looking for.

User avatar
jscher2000
2StarLounger
Posts: 148
Joined: 26 Dec 2010, 18:17

Re: Using the Selected Index

Post by jscher2000 »

Joseph wrote:The object ("ctl00_cphMain_ddlLocation") is a drop down box. Rather than iserting the value "84", I think I need to use the 'selected index'. I just do not know how. Could someone please give me an example.
I don't code in ASP.Net, but the general principle should be the same...

To set the <select> to a particular <option>, you can use:

Code: Select all

SelectObject.selectedIndex = optionNumber
Note that options are numbered from zero.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Using the Selected Index

Post by agibsonsw »

HansV wrote:Does this work?

.document.all("ctl00_cphMain_ddlLocation").Items.FindByValue("84").Selected = True
Should this be ct100_ (one hundred)? Looks like a 1 in the original post.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

agibsonsw wrote:Should this be ct100_ (one hundred)? Looks like a 1 in the original post.
No, it's a lower case L, as you can verify by copy/pasting it into Word or Notepad.
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

Wow thanks. This code:

Code: Select all

.Document.GetElementById("ctl00_cphMain_ddlLocation").selectedindex = 1
Does change the index to what I want without error, however it does not fire to activate the remaining objects. Is there a way to fire the event?

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: Using the Selected Index

Post by Joseph »

Here is the code in full....

Code: Select all

Private Sub CommandButton1_Click()
Dim IE As Object
Set IE = CreateObject("internetexplorer.application")
   
With IE
        .Visible = True
        .navigate ("http://rlgwsweb01h1.roadlink.net:90/reports/loadbylocation2rebate_DataT.aspx")
        While .Busy Or .readyState <> 4: DoEvents: Wend
        
'set location
        Set L = IE.document.all("ctl00_cphMain_ddlLocation")
  
'set department
        Set D = IE.document.all("ctl00_cphMain_lbDepartment")
  
'set from date
        Set F = IE.document.all("ctl00_cphMain_txbFromDate")
  
'set to date
        Set D = IE.document.all("ctl00_cphMain_txbToDate")
  
'set rate group
        Set R = IE.document.all("ctl00_cphMain_lbRateGroup")
  
'Automate Location
        
        L.fireevent ("onchange")
        L.selectedindex = 1
   
    End With
    
End Sub

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Using the Selected Index

Post by agibsonsw »

Case sensitivity might be a problem. Try fireEvent and selectedIndex.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
jscher2000
2StarLounger
Posts: 148
Joined: 26 Dec 2010, 18:17

Re: Using the Selected Index

Post by jscher2000 »

Joseph wrote:This code:

Code: Select all

.Document.GetElementById("ctl00_cphMain_ddlLocation").selectedindex = 1
Does change the index to what I want without error, however it does not fire to activate the remaining objects. Is there a way to fire the event?
If the <select> has an onchange event handler, it should fire automatically when you change the selectedIndex.At least, it works that way in the non-automated context (e.g., in a web page containing client-side script). If you turn on display of script errors in IE, does anything pop up?

Edit: Actually, no, I seem to be wrong about that. You'll have to run the code yourself. No need to fire the event if you can just call the function directly.

Edit #2: In a favelet, SelectObject.fireEvent("onchange") does seem to work. After you change the selectedIndex, of course.

User avatar
HansV
Administrator
Posts: 78626
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using the Selected Index

Post by HansV »

agibsonsw wrote:Case sensitivity might be a problem. Try fireEvent and selectedIndex.
JavaScript is case-sensitive, but this is VBA in Excel, automating Internet Explorer. VBA is not case sensitive.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Using the Selected Index

Post by agibsonsw »

HansV wrote:JavaScript is case-sensitive, but this is VBA in Excel, automating Internet Explorer. VBA is not case sensitive.
I was aware of this. But isn't it the JavaScript interpreter of the browser that translates/actions the 'fireEvent' function?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.