Excel VBA: Click javascript anchor on webpage through IE and save generated file

JLaps

New Member
Joined
Apr 3, 2014
Messages
4
Greetings,

I've been reading this forum for a couple years now, but this is my first time posting a question. As they say on the radio call-in shows, "First-time, Long-time."
While I've grown leaps and bounds in my general excel macro writing, I am just beginning to delve into automating Internet Explorer through excel vba. I've spent many hours now trying to find the exact solution to what I'm trying to do, and I've hit a bit of a wall (perhaps I'm just not searching for the right question).

Windows version: Windows 7
Excel version: Excel 2013

Problem: I am attempting to download a file from a webpage using excel vba. The file is generated by clicking on a link which I believe runs some java script. I've figured out how to navigate to the page, and click the link. However at this point, it pops up a "Save Or Open file" box at the bottom of the page and I couldn't quite figure out how to get around this. i've seen multiple suggestions and code snippets, however none of them worked unless there was a direct URL link to the file.

Code:
Code:
Sub GetDat()
  
  Dim IE As InternetExplorer
  Dim link As Object
  Dim LinkCount As Integer
  Dim state As Integer

    Set IE = New InternetExplorer
    IE.Visible = True


  IE.Navigate "http://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=y&type=1&season=2013&month=13&season1=2013&ind=0&team=0&rost=0&age=0&filter=&players=0#custom"

LinkCount = 0
state = 0
Do Until state = 4
    DoEvents
    state = IE.ReadyState
Loop

For Each link In IE.Document.Links
   If link.innertext = "Export Data" Then
   IE.Document.Links(LinkCount).Click
  Exit For
   End If
   LinkCount = LinkCount + 1
Next link


End Sub


The HTML for the link itself is:

HTML:
<a id="LeaderBoard1_cmdCSV" href="javascript:__doPostBack('LeaderBoard1$cmdCSV','')">Export Data</a>


It's entirely possible I'm missing a simple solution here as my understanding of HTML, java, and general web-based stuff is very very basic. Any code examples or even a nudge in the right direction so I can figure it out myself would be greatly appreciated.

Thanks in advance for any assistance, and let me know if more information or clarification is required.

JL
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You could automate the page in IE browser and then click buttons and enter text on the IE download windows using Windows API functions. For an example of this technique see http://www.mrexcel.com/forum/excel-...ile-download-dialog-box-without-sendkeys.html. Note that the code was written for IE8 and though I've never tried, I think it would need changing for IE9 and higher.

With the page being .aspx, a far better and faster approach is to send the page's form data with appropriate parameter names and values in a XMLhttp POST request and receive the file bytes in response. For an example, see http://www.mrexcel.com/forum/excel-...n-internet-explorer-web-site.html#post3404965.
 
Last edited:
Upvote 0
John_w,

Thanks for the response! While I am always in favor of a faster and better approach, the code in your second example is beyond my current skill level to edit. I'd like to fully understand any code I'm going to be running, and after spending some time looking into exactly how this works, it has occurred to me that I probably have a ways to go before I'd really grasp it.

The first example however, as unwieldy as it may be in comparison, is more on my level of coding experience and I think I can work with that. The issue I'm running up against here is that I'm using IE11 and this version no longer pops up the save/save as/open window that was used in previous versions, it pops up a ribbon at the bottom of the window which seems to function differently. In fact I found someone else having the same issue in a thread here Internet Explorer 11: File Download Dialog not appear where "FindWindowEx" doesn't work to access the save dialog box. While there is no direct solution proposed in that thread, it does mention how to access it by keystroke, however I have read everywhere that .sendkeys is only to be used as a last resort.

I'll tinker around with it and see what I can figure out. Thanks again for your reply.

JL
 
Upvote 0
jharaldson,

I wish I could say I persevered and figured out an optimal solution, however my schedule lately has not allowed for that (hooray for being a father), so I just cobbled together something temporary using sendkeys.

JL
 
Upvote 0
well, I have been looking into it as well, for hours actually and haven't found anything to implement this really. If you guys do, it would be nice if you posted it here. thanks
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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