How to Click on IE link using VBA

dleads
dleads used Ask the Experts™
on
I am able to navigate to a webpage that lists all of the courts.  Once there, my vba code determines which court to go to.  I find it in the list, but then I do not know how to get it to click on that link.  

This is an example of the source for one of the courts I'd like to click on.  My code finds the 'Alabama Northern District Court', but then how do I click it?

                      I need to grab ....https://ecf.alnd.uscourts.gov.... and select that link

strfoundcourtname = ?grab the https link somehow
ie.Document.all.Item(strfoundcourtname).Click  
How would I do that?

 <TR valign=center align=left>
                <TD><IMG src=/images/pscovl1.gif hspace=10 alt=*>
                    <A href=https://ecf.alnd.uscourts.gov/ class=linktxt>
                    Alabama Northern District Court  </A>                    - <font size="1" color="#800517"><b>CM/ECF-DC V3.2.1</b></font>                    <A href=http://pacer.psc.uscourts.gov/psco/cgi-bin/courtinfo.pl?court=E_ALNDC TITLE="Court Information" class=linktxt>                    <IMG STYLE="border: none;" SRC="/images/courtinfo.png" ALT="Court Info"></A>

            </TD></TR>

TIA
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
dleads,,

If the page is already "Selected", why can't you click it manually.

If you can select the link, then you should be able to get it ino vba, then activate it using the FollowHyperlink Command.

Something like this (asssuming you have a textbox with a valid webpage in it):

Dim hlnk As String
hlnk = Me.txtHL
    Application.FollowHyperlink hlnk

JeffCoachman

Author

Commented:
Thanks for repsonding Jeff.
I am attempting to automate the selection of a link on a webpage (that has many links listed).  
The example I provided shows just one state (Alabma).  My Access form will state which State is required.  The user will then click a button that will go to the site, find the State in the list and then go to that particular State's website.  I need to figure out how to get the hyperlink of the state and click on it.

So my question is this... How would I code VBA to to find a specific state, in this case Alabama, and click on the hyperlink.  The source code of the webpage, for just this Alabama example, is as follows:
<TR valign=center align=left>
                <TD><IMG src=/images/pscovl1.gif hspace=10 alt=*>
                    <A href=https://ecf.alnd.uscourts.gov/ class=linktxt>
                    Alabama Northern District Court  </A>                    - <font size="1" color="#800517"><b>CM/ECF-DC V3.2.1</b></font>                    <A href=http://pacer.psc.uscourts.gov/psco/cgi-bin/courtinfo.pl?court=E_ALNDC TITLE="Court Information" class=linktxt>                    <IMG STYLE="border: none;" SRC="/images/courtinfo.png" ALT="Court Info"></A>

            </TD></TR>

How would I programmatically find the "<A href=https://ecf.alnd.uscourts.gov/ class=linktxt>" and click on the link?  
I do not want the user to manually click on it - I'm trying to get it all done through automation.

Thanks!
Commented:
Hi dleads
and another hi goes to my friend Jeff :)

I would like to point you to 2 links to solve this issue:
1.
extract the TAG IDs for all the links/info/dat of a web site,
then to check if the info/data you are looking for has a "tag ID" OR a "character reference",
if it has, then you can get the name or the reference, and apply your code:
ie.Document.all.Item(strfoundcourtname).Click
Please see the 2nd post:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_21991495.html

2.
however, I can see from your post above, that the site you are on, does not TAG the data/field names,
so we'll have to pars the HTML page, looking for EXACTLY what you are looking for, like this:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_23045378.html#21014775
then when the like is extracted, and assigned to the variable strfoundcourtname, use this code:
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate strfoundcourtname

Ok, that was the fun part, let's get into the serious part now :)
How can your Form identify 'Alabama Northern District Court' web site from this site?
The answer should be from this info from the link you posted:
<A href=https://ecf.alnd.uscourts.gov/ class=linktxt> Alabama Northern District Court  </A>
So in this case, in your Form, when you select 'Alabama Northern District Court'
the code should look for the EXACT spell on the web page, like this:

    'copy the IE page source to the variable vBody
    vBody = IE.Document.body.innerHTML

'find the 1st occurance of Court name. in the Form this control is named txt_CourtName
vNumber1 = InStr(vBody, me.txt_CourtName)
'find the 1st occurance of '' AFTER the earlier line,
'now read backwords, and find out where does '<A href=' start
vNumber2 = InStrRev(vNumber1, vBody, "<A href=")
'now lets read between the 2 values we got above:
Court_Link=Mid(vBody, vNumber2 + 8, (vNumber2+8) - vNumber1)
Court_Link=replace(Court_Link,">","")
'Now Court_Link has the link, let's see if it is exactly what we want:
msgbox "+" & Court_Link & "+"
'the reason I added the + on both ends, is so that we can see if there are extra spaces too, to trim
''if ok, then us:
IE.Navigate Court_Link
 
Ah, I hope this "Air code" will work for you :)
 
jaffer
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
jjafferr,

Hey, thanks for coming in on this Q.

I was hoping an Expert with more Web experience than I would chime in.
;-)

JeffCoachman

Author

Commented:
Thank you!   I just had to change the code slightly.

'copy the IE page source to the variable vBody
   vBody = ie.Document.Body.innerHTML

'find the 1st occurance of Court name. in the Form this control is named txt_CourtName
vnumber1 = InStr(vBody, Me.Company)
'now read backwords, and find out where does '", "")

'Now Court_Link has the link, let's see if it is exactly what we want:
'MsgBox "+" & Court_Link & "+" 'the reason I added the + on both ends, is so that we can see if there are extra spaces too, to trim
court_link = Trim(court_link)

'now go to the right court
ie.navigate court_link & "/cgi-bin/login.pl"

Commented:
Hi dleads

Well, I am glad you got it working :)

I have a few comments about your changes:
>court_link = Trim(court_link)
The Trim here will delete ALL the empty spaces, not only the last empty space,
thus making your ink not useble, so I recommend:
court_link = RTrim(court_link)

>ie.navigate court_link & "/cgi-bin/login.pl"
                                         ^^^^^^^^^^
Although this will work for this particular page, but what about the page of the other courts?
Unless off course it is common for all the courts.

jaffer
ps you are welcome Jeff :)

Author

Commented:
Excellent point about the trim.  Thank you.
Yes, the /cbi-bin/login.pl is how you get to the login for all of the courts.

I appreciate your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial