Using VBA to click a link on a page

frustrating

Registered User.
Local time
Today, 12:10
Joined
Oct 18, 2012
Messages
68
Hello:

I am trying to figure out the best way to automate the click of a link.

Basically what I want is to open the browser to a specific page and search for a pdf file (which is named arbitrarily) and click on it.

What I have here:
Code:
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "whatever link"
IeApp.navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
 
Set IeDoc = IeApp.Document
For Each Element In IeDoc.links
    If Element.innerText = ".pdf" Then
        Call Element.Click
        Exit For
    End If
    Next Element
Isn't working. Any ideas? It loads the page fine, but won't click on a link.
 
I adjusted my code a bit, and now it at least finds the link:
Code:
Private Sub Text282_Click()
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "link"
IeApp.navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
 
Set IeDoc = IeApp.Document
For Each Element In IeDoc.links
    If InStr(Element.innerText, ".pdf") Then
        Call Element.Click
        Exit For
    End If
    Next Element
End Sub
But the opened browser closes immediately after the link gets clicked! Weird.
 
Last edited:
That was an very interesting post. Sorry, I didn't find the exact answer for you.
If you happen to get the .PDF download link open, this might be the next step you are looking for. Thought it might be helpful for later.

http://www.siddharthrout.com/2011/1...-opensavecancel-button-on-ie-download-window/
http://www.siddharthrout.com/2012/0...ecancel-button-on-ie-download-window-part-ii/

It's funny you linked that site. I use that script for another part of my application I'm using (and I will be using it with this part too). For anyone who needs to automate the Save As/ Open features with hwind, that is an awesome script.

I just need to be able to click this stupid link without the browser inexplicably closing. ARGH!

I'm wondering it has to do with the fact that it's going to a direct file which opens a popup and not a normal html file. I'm still working on it. If anyone has any ideas, let me know! I'll report back if I find the solution.
 
I just wanted to bump this to see if anyone had any ideas as to why the element.click would open a browser that immediately crashes.
 

Users who are viewing this thread

Back
Top Bottom