简体   繁体   English

是否可以通过使用 Excel 宏单击下载按钮来下载 CSV 文件?

[英]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.我想在 Excel 中编写一个宏,该宏将从我用于工作的 Web 应用程序下载 CSV。 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. Web 应用程序的用户界面让您单击按钮打开菜单,然后单击弹出菜单中的下载按钮。

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.我编写了一个宏来打开 Internet Explorer 窗口,然后单击下载按钮以下载我想要下载的 CSV 文件。 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.我还不能让它工作:它打开浏览器到我想要的网页,但它不下载 CSV。

I got the below HTML by using "Inspect Element" (I cut out parts that didn't seem relevant).我通过使用“检查元素”得到了下面的 HTML(我剪掉了看起来不相关的部分)。 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.我对 Blob URL 的工作方式不是很熟悉,但我认为下载 URL 会发生变化。 (The URLs I have in the code/HTML are obviously not the real URLs). (我在代码/HTML 中的 URL 显然不是真正的 URL)。

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.编辑:下面是按钮的 HTML,必须单击该按钮才能展开包含“导出到 CSV”选项的菜单。 The important part begins at <button class="MuiButtonBase-root MuiIconButton-root"重要的部分开始于<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!首先:每个代码模块中始终使用Option Explicit作为第一行!

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.您可以使用Set elements = objIE.Document.getElementsByClassName("downloadLinkBut​​ton")(0)直接获取链接,如果它是文档中与此 CSS 类的唯一链接。

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你可以用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.第四:如果显示的行产生错误,我认为你不能在弹出的 html 代码上工作,因为链接是。

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?但是在这里你可以看看如何通过代码生成页面、HTML 事件和(我认为你会需要它)SendKeys(): 如何使用 excel vba 单击交互式对话框弹出来解决这些问题?

If you get the downlad link you can also use API function URLDownloadToFile() instead of the click and SendKeys().如果您获得下载链接,您还可以使用 API 函数 URLDownloadToFile() 而不是 click 和 SendKeys()。 SendKeys() is in the most cases a realy bad solution. SendKeys() 在大多数情况下是一个非常糟糕的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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