PDA

View Full Version : [SLEEPER:] VBA Code for clicking a link?



igotgame
03-15-2009, 07:12 PM
Guys I am having some trouble.

I am looking at web page source below:

<TR><TD><A HREF="javascript:Needed('BorrInfo')"><IMG SRC="../images/ToDoNeeded.gif" WIDTH="10" HEIGHT="10" ALIGN="BOTTOM" border=0></A> <A HREF="javascript:GoTo('BorrInfo')"><font color=BLACK>Borrower Information</FONT></A></TD></TR>

There is no ID and no Name...so how can I reference it in order for it to be clicked? What code would I use here?


I can get it to work easily on Google.com using the code below, but being that the above page source code is javascript what would be different?:


Sub ClickLink()
' ClickLink Macro
' Macro recorded 3/15/2009 by Game
Dim htmlDoc As MSHTML.HTMLDocument
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate "https://www.google.com"
.Visible = 1
Do While .Busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
Set htmlDoc = .Document
Set htmlColl = htmlDoc.getElementsByTagName("A")
For Each htmlInput In htmlColl
If htmlInput.innerText = "Images" Then
htmlInput.Focus
htmlInput.Click
Exit For
End If
'Debug.Print "Frame " & x & ": " & htmlInput.innerText
Next htmlInput
End With
End Sub

Kenneth Hobs
03-16-2009, 05:35 AM
It may not be possible to do. Have you tried iterating through the ALL collection?

Maybe submitting the form would help. e.g.

'http://www.ozgrid.com/forum/showthread.php?t=95502
Sub Yahoo()
'This project includes references to "Microsoft Internet Controls", shdocvw.dll and
'"Microsoft HTML Object Library", mshtml.tlb
'Variable declarations
Dim myIE As New InternetExplorer 'New '
Dim myURL As String
Dim myDoc As HTMLDocument
Dim strSearch As String, str As String
Dim itArray() As String
'Set starting URL and search string
myURL = "http://finance.yahoo.com/lookup?s=TOT"
strSearch = "TOT"
'Make IE navigate to the URL and make browser visible
myIE.navigate myURL
myIE.Visible = False
'Wait for the page to load
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
'Set IE document into object
Set myDoc = myIE.document
'Enter search string on form
'myDoc.forms(0).UserName.Value = strSearch
'Submit form
myDoc.forms("quote").submit
'Wait for the page to load
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
str = myDoc.getElementById("yfi_key_stats").innerText
itArray() = Split(str, vbCrLf)
[a1] = "Dividend Date:"
[B1] = Right(itArray(3), 9)
Set myIE = Nothing
Set myDoc = Nothing
End Sub

igotgame
03-16-2009, 06:10 AM
I actually have it pulling this "javascript:GoTo('BorrInfo')" into a variable..

I just don't know how to execute it.

htmlInput.Javascript = Execute?

I have no idea how to execute it.