【问题标题】:how to download and save a document by VBA如何通过 VBA 下载和保存文档
【发布时间】:2017-04-10 13:57:27
【问题描述】:

我正在尝试替换我每天下载的文档。但是我不知道点击下载按钮后我需要做什么。我需要在我的文档中保存具有特定名称的文档

Dim IE As Object
    Dim n, Period1, Period2 As Double



    'retorna o internet explorer-return the correct period
    Period1 = "201612"
    Period2 = "201612"

    'abre o internet explorer
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "http://www2.susep.gov.br/menuestatistica/SES/principal.aspx"
    IE.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))
    'seleciona as operações desejadas
    IE.document.getElementById("ctl00_ContentPlaceHolder1_edSelProd").SelectedIndex = "8"
    IE.document.getElementById("ctl00_ContentPlaceHolder1_btnConsultar").Click

    'seleciona o periodo
    Application.Wait (Now + TimeValue("00:00:02"))
    Set ieDoc = IE.document
        ieDoc.getElementById("ctl00_ContentPlaceHolder1_edInicioPer").Value = Period1
        ieDoc.getElementById("ctl00_ContentPlaceHolder1_edFimPer").Value = Period2

    'seleciona as empresas
    IE.document.getElementById("ctl00_ContentPlaceHolder1_edEmpresas").SelectedIndex = "0"

    ieDoc.getElementById("ctl00_ContentPlaceHolder1_Button1").Click

    Application.Wait (Now + TimeValue("00:00:02"))
    ieDoc.getElementById("ctl00_ContentPlaceHolder1_Button1").Click

【问题讨论】:

  • 我一直发现使用 Internet Explorer 自动化下载很困难,因为 IE 通知询问您是要保存还是打开文件 - 据我所知无法关闭。相反,我建议通过 Shell 命令(在 VBA 中)运行 Google Chrome 或 Firefox,因为它们没有这样的通知框,或者如果它们有,它们可以被禁用。

标签: vba excel internet-explorer


【解决方案1】:

我已经通过以下方式实现了一些目标:

Option Explicit

Sub TestMe()


    Dim IE As Object
    Dim n As Double, Period1 As Double, Period2 As Double
    Dim ieDoc As Object

    'retorna o internet explorer-return the correct period
    Period1 = "201612"
    Period2 = "201612"

    'abre o internet explorer
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "http://www2.susep.gov.br/menuestatistica/SES/principal.aspx"
    IE.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))
    'seleciona as operações desejadas
    IE.document.getElementById("ctl00_ContentPlaceHolder1_edSelProd").SelectedIndex = "8"
    IE.document.getElementById("ctl00_ContentPlaceHolder1_btnConsultar").Click

    'seleciona o periodo
    Application.Wait (Now + TimeValue("00:00:02"))
    Set ieDoc = IE.document
        ieDoc.getElementById("ctl00_ContentPlaceHolder1_edInicioPer").value = Period1
        ieDoc.getElementById("ctl00_ContentPlaceHolder1_edFimPer").value = Period2

    'seleciona as empresas
    IE.document.getElementById("ctl00_ContentPlaceHolder1_edEmpresas").SelectedIndex = "0"

    ieDoc.getElementById("ctl00_ContentPlaceHolder1_Button1").Click

    Application.Wait (Now + TimeValue("00:00:03"))
    ieDoc.getElementById("ctl00_ContentPlaceHolder1_Button1").Click

    AppActivate IE.name, 1
    SendKeys "%{s}"
    SendKeys "{ENTER}"

End Sub

玩一下 sendkeys,在巴西(葡萄牙语)它们应该不同于“s”。在这里阅读更多: https://msdn.microsoft.com/en-us/library/office/ff821075.aspx

我的想法是导航到 SaveAs 按钮(在葡萄牙语中,它是别的东西)并使用 SendKeys 来获取它。

这样的事情应该是可能的:

AppActivate IE.Name, 2
SendKeys "{TAB}{TAB}"
SendKeys "{DOWN}"
SendKeys "%{a}"
SendKeys "{ENTER}"

【讨论】:

  • 那么你应该尝试导航到SaveAs 并给出你自己的名字。不知何故。使用{TAB}{TAB}{TAB} 或类似的东西。
  • 当你说另存为时,是不是像写'Report = Application.GetSaveAsFilename("Attrition Report.xls', "Excel Files (*.xlsx), *.xlsx") "?你能为我写信吗?我没有其他想法
  • 嘿,@Vityata 你能帮帮我吗?你有什么想法吗?
  • @QHarr - 如果您没有明确标记用户,那么只有知道答案的人才能看到消息。
  • @Vinicius - 请考虑接受给出的答案
猜你喜欢
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
相关资源
最近更新 更多