简体   繁体   中英

Is it possible to download a CSV file by using an Excel macro to click on a download button?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM