2017-04-10 90 views
1

我试图替换每天下载文档。但我不知道在点击下载按钮后我需要做什么。我需要的文档具有特定名称保存在我的文档如何通过VBA下载和保存文档

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 
+0

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

回答

0

我已成功地实现了与下面的内容:

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

我的想法是,导航到另存为按钮(在葡萄牙,它的别的东西),并使用SendKeys得到它。 enter image description here

像这样的东西应该是可能的:

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

然后你应该尝试导航到'SaveAs'并给出你自己的名字。不知何故。使用“{TAB} {TAB} {TAB}”或类似的东西。 – Vityata

+1

当你说SaveAs时,它是否像写'报告= Application.GetSaveAsFilename(“Attrition Report.xls”,“Excel文件(* .xlsx),* .xlsx”)“? 你能帮我写吗?我没有任何其他想法 – Vinicius

+0

嘿,@Vityata 你能帮我吗?你有什么主意吗? – Vinicius

相关问题