Solution 1 :

First of all: Use always the line Option Explicit as first line in every code module!

Second: You don’t need the loop to find the download link. You can use Set elements = objIE.Document.getElementsByClassName(“downloadLinkButton”)(0) to get the link directly, if its the only link with this CSS class in the document.

Third: It can be the page needed more time to load completly, because there are information which must load too. Then you need a little break. You can do this with Application.Wait

Fourth: If the shown line generates an error, I think you don’t work on the pop up html code were the link is.

Try this:

Option Explicit

Sub DownloadCSV()

Dim objIE As InternetExplorer 'special object variable representing the IE browser
'Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim elements As Object

  'initiating a new instance of Internet Explorer and asigning it to objIE
  Set objIE = New InternetExplorerMedium

  'make IE browser visible (False would allow IE to run in the background)
  objIE.Visible = True

  'navigate IE to this web page
  objIE.navigate "exampleURL.com"

  Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
  'Break for 5 seconds to looad more data if needed
  Application.Wait (Now + TimeSerial(0, 0, 5))

  Set elements = objIE.Document.getElementsByClassName("downloadLinkButton")(0)

  'Check whether the variable elements contains a HTML element
  'with the CSS class "downloadLinkButton"
  If Not elements Is Nothing Then
    'Wanted element found
    elements.Click
  Else
    'Wanted element not found
    MsgBox "There is no Element with the CSS class 'downloadLinkButton' in the used HTML document"
  End If
End Sub

I don’t know what kind of pop up it is on your site. But here you can take a look how those kind of problems can be solved with code generating pages, HTML events and (I think you will need it) SendKeys():
How to use excel vba to click on interactive dialog pop-up?

If you get the downlad link you can also use API function URLDownloadToFile() instead of the click and SendKeys(). SendKeys() is in the most cases a realy bad solution.

Problem :

I want to write a macro in Excel that will download a CSV from a web application I use for my job. The user interface for the web application has you click on a button to open a menu, then click on a download button within the pop-up menu.

I wrote a macro that’s supposed to open an Internet Explorer window, then click on the download button in order to download the CSV file I want to download. I haven’t been able to get it to work yet: It opens the browser to the webpage I want, but it doesn’t download the CSV.

I got the below HTML by using “Inspect Element” (I cut out parts that didn’t seem relevant). Note the download button at the end.

    <body style="overflow: hidden; padding-right: 8px;">
            <div role="presentation" class="MuiPopover-root" ... left: 0px;">
                    <div class="MuiPaper-root MuiMenu-paper MuiPaper-elevation8 MuiPopover-paper
                            MuiPaper-rounded"… transform-origin: 0px 26px;">
                    <ul class="MuiList-root MuiMenu-list MuiList-padding" role="menu" tabindex="-1">
                    ...
                    <a download="FileName.csv" class="downloadLinkButton" target="_self" href="blob:https://exampleURL.com/abcdefg1234">
                            <li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
                             tabindex="-1" role="menuitem" aria-disabled="false">Export to CSV</li>
                    </a>

This is the code that I’ve written, but it doesn’t work:

    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorerMedium

    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True

    'navigate IE to this web page
    objIE.navigate "exampleURL.com"


    Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop

    Set elements = objIE.Document.getElementsByTagName("a")

    For Each ele In elements
        If (ele.className = "downloadLinkButton") Then
            ele.Click
            Exit For
        End If
    Next ele

As I mentioned, this macro doesn’t error out, it just opens the browser to the webpage I want.

Does anyone have advice on how I could automate this download?
I’m not very familiar with how Blob URLs work, but I think that the download URL changes. (The URLs I have in the code/HTML are obviously not the real URLs).

Thank you!

Edit: Below is the HTML for the button which must be clicked to expand the menu that contains the “Export to CSV” option. The important part begins at <button class="MuiButtonBase-root MuiIconButton-root"

<div class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-8">
        <div class="MuiGrid-root MuiGrid-container MuiGrid-direction-xs-column MuiGrid-align-items-xs-flex-end">
                <div class="icon-container">
                        <span class="lastupdated-container…</span>
                         …
                        <button class="MuiButtonBase-root MuiIconButton-root" tabindex="0" type="button" id="card-view-more" aria-label="More">
                                <span class="MuiIconButton-label">
                                        <span class="material-icons MuiIcon-root" aria-hidden="true">more_vert</span>
                                </span>
                        </button>
                </div>
        </div>
</div>

Comments

Comment posted by alowflyingpig

What happens after the

Comment posted by alphabetSoup555

The click isn’t getting triggered. I think the

Comment posted by alphabetSoup555

Thank you so much for your response! I used some of your code to confirm that Excel is

Comment posted by alphabetSoup555

Under in the HTML it has

Comment posted by Zwenn

I think there can be two kinds of pop up. Either another browser window opens or the code of the page is changed to show a menu. You have to react to the technique used. But I can’t do this for you because I don’t know the page. Which technique is used?

Comment posted by alphabetSoup555

A window doesn’t pop up: When you click on an ‘open menu’ button, a menu expands. This menu contains the “Export to CSV” option. When the ‘open menu’ button is clicked, the page’s html changes: the

Comment posted by Zwenn

I’m still not sure if you can already open the popup automatically or not. The link in my answer below the macro shows you how it works when it needs to be triggered by an HTML event. But you have to know which event. Open the page in your browser and press F12. Click through the HTML source code in the DOM inspector until you reach the button that has to be triggered for the popup. Look in the DOM Inspector to see if there is a small button labeled Events at the end of the button code. Click on it and tell me which events are displayed.

By