2017-04-10 89 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

相關問題