IE and VBA radio button selection

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
I am simply trying to select the Last Name and State radio button so I can then move on with the rest of my routine. Any help would be appreciated.

I am having issue with this piece of code:
Code:
Ie.Document.getelementsbyname("L")(1).Click

Here is the below HTML I am sourcing from:
HTML:
  Find a provider by entering some or all of their L

Here is the actual routine I need help with:
Code:
Sub Medicare()
    Dim Ie
    Set Ie = CreateObject("InternetExplorer.application")
    Ie.Visible = True
    '##############################################################################
    '*******************Choose Physician or Other HealthCare Provider**************
    '##############################################################################
    Ie.navigate ("[URL]http://www.medicare.gov/Physician/Search/chooseprovider.asp[/URL]")
    Do
        If Ie.readyState = 4 Then
            Ie.Visible = True
            Exit Do
        Else
            DoEvents
        End If
    Loop
     'USE VIEW SOURCE TO GET FORM ELEMENT IDS
    Ie.Document.getelementsbyname("L")(1).Click
End Sub
</STRONG></LABEL>
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The following Code Fragment does not resolve to an object:

Rich (BB code):
Ie.Document.getelementsbyname("L")(1).

Therefore, you cannot invoke the Click method.

While the GetElementsByName does indeed return an array, for some strange reason that I have never been able to find an explanation for, the only method that seems to allow you to coerce a single member to an object is the For..Each structure. I can't explain it, but it works (even though it's not efficient.

Per teh Source Code for that page, the Name of the checkbox item that you are looking for is "Type", and it's NOT the first member of the array.

Try the following code (I had to step through from the first page, rather than navigating directly to teh page of interest).

Rich (BB code):
Sub Medicare()
    'reference: Microsoft Internet Controls
    'reference: Microsoft HTML Object Library
    
    Dim Ie As InternetExplorer
    Dim arr, a
    
    Set Ie = New InternetExplorer
    Ie.Visible = True
    '##############################################################################
    '*******************Choose Physician or Other HealthCare Provider**************
    '##############################################################################
    Ie.navigate ("http://www.medicare.gov/Physician/Search/PhysicianSearch.asp")
    Do
        If Ie.readyState = READYSTATE_COMPLETE Then
            Ie.Visible = True
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
    Do Until Ie.document.readyState = "complete"
    
    Loop
     'USE VIEW SOURCE TO GET FORM ELEMENT IDS
    Set arr = Ie.document.getElementsByName("btnSubmit")
    
    For Each a In arr
    
        a.Click
    
    Next a
    
    Do Until Ie.document.readyState = "complete"
    
    Loop
    
    Set arr = Ie.document.getElementsByName("btnSubmit")
    
    For Each a In arr
    
        If a.Value = "Find a Physician" Then
        
            a.Click
            
            Exit For
        
        End If
    
    Next a
    
    Do Until Ie.document.readyState = "complete"
    
    Loop
    
    Set arr = Ie.document.getElementsByName("Type")
    
    For Each a In arr
    
        If a.Value = "STATE" Then
        
            a.Click
            
            Exit For
        
        End If
    
    Next a
    
End Sub

You also need to be aware of the difference between the Readystate Property of the IE Session, and that of the Document Object. You'll notice that I added a second loop to wait for the Document to load in the IE Session. And after each time you press a button, you need to wait for the Document to reload.
 
Upvote 0
First let me thank you for taking the amount of time you did to pull this solution together.

Currently I get a complie error on this code snippet (user-defined type not defined):

Code:
Dim Ie As InternetExplorer

Is the nature of the code you worked out require the user to click through each button or is VBA taking care of all clicking of buttons?

Up till this point we have been using the sendkeys function to automate this process and wanted a more stable process so we wanted to use the HTML form elements as the guidepost.

Let me know your thoughts...
 
Upvote 0
This method clicks through all o fteh buttons up to the page that were you were apparently referencing in your initial question. Sendkeys is very unstable,as you have discovered. This method may be a little cludgey at times in teh resulting code structure, but it's very stable.

Please refer to the 2 lines at the top my my code: you must add 2 references to your project so the InternetExplorer library as well as the enumerations and the Intellisense model are available at design-time. This approach (versus runtime binding) will make your developement much easier. You can swap back to runtime binding when the project is fully developed, but that's up to you.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top