如何通过VBA下载和保存文档

时间:2017-04-10 13:57:27

标签: excel vba excel-vba internet-explorer

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

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

1 个答案:

答案 0 :(得分: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来获取它。 enter image description here

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

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