2015-08-21 107 views
1

我們需要自動從NASDAQ網站下載文件。我現有的VBA代碼打開一個IE「你想打開/保存」對話窗口。如何點擊該保存按鈕並通過VBA提供路徑? 我也試過在這個鏈接here中描述的各種Windows API方法,但是這是給出了「未找到窗口」的結果。在VBA中控制IE11「您想打開/保存」對話窗口按鈕

我當前的代碼如下:

Sub MyIEauto() 

    Dim ieApp As InternetExplorer 
    Dim ieDoc As Object 
    'Dim ieTable As Object 

    'create a new instance of ie 
    Set ieApp = New InternetExplorer 

    'you don’t need this, but it’s good for debugging 
    ieApp.Visible = True 
    'assume we’re not logged in and just go directly to the login page 
    ieApp.Navigate "https://indexes.nasdaqomx.com/Account/LogOn" 
    Do While ieApp.Busy: DoEvents: Loop 
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

    Set ieDoc = ieApp.Document 
    'fill in the login form – View Source from your browser to get the control names 
    With ieDoc.forms(0) 
     .UserName.Value = "xxxxxxx" 
     .Password.Value = "xxxxxxx" 
     .submit 
    End With 
    Do While ieApp.Busy: DoEvents: Loop 
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

    'now that we’re in, go to the page we want 
    ieApp.Navigate "https://indexes.nasdaqomx.com/Index/ExportWeightings/NDX?tradeDate=2015-08-19T00:00:00.000&timeOfDay=SOD/SODWeightings_2015" 

    'next below line commented as it is failing 
    'ieApp.ExecWB 4, 2, "D:\VBA code work\SODWeightings_20150819_NDX.xlsx" 

    set ieApp=Nothing 
    set ieDoc=Nothing 

End Sub 

下面的截圖顯示我已經到了。我如何從這裏進步?

enter image description here

回答

3

另一種方式做,這是送的快捷鍵的按鍵點擊在IE11保存按鈕。我應該注意你的IE窗口將需要成爲這個工作的活動窗口。因此,它在調試模式下不起作用。

下面的代碼調用快捷鍵。我只是顯示快捷鍵,以便您更好地瞭解發生了什麼。

  • 快捷鍵:Alt鍵+小號
  • VBA:Application.SendKeys "%{S}"
+0

我很困惑。事件雖然我的IE窗口是「激活」,以便廢除它的HTML,但這對我不起作用。我很困惑,因爲它在我跑過的30個測試中運行了兩次,我不確定爲什麼 – Seb

+0

@Seb當我說激活時,我的意思是窗口有焦點。如果不同的窗口具有焦點,則SendKeys將轉到該窗口。 –

5

它終於解決了......

Option Explicit 

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ 
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ 
ByVal lpsz2 As String) As Long 

Public Sub AddReference() 

    ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\SysWOW64\UIAutomationCore.dll" 

End Sub 

'after my original code as posted in question then this below lines 

Dim o As IUIAutomation 
    Dim e As IUIAutomationElement 
    Set o = New CUIAutomation 
    Dim h As Long 
    h = ieApp.hWnd 
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) 
    If h = 0 Then Exit Sub 

    Set e = o.ElementFromHandle(ByVal h) 
    Dim iCnd As IUIAutomationCondition 
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save") 

    Dim Button As IUIAutomationElement 
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd) 
    Dim InvokePattern As IUIAutomationInvokePattern 
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId) 
    InvokePattern.Invoke 
+0

這是陰涼恭喜查找新解決這一;你是否願意詳細闡述/解釋答案,以便將來對其他有類似問題的人更有幫助? –

+0

我試圖在這裏使用Windows API,參考如下https://msdn.microsoft.com/en-us/library/system.windows.automation.invokepattern(v=vs.110).aspx – pmr

+0

Awsome !!只需要添加-DIA UIAutomationCore.dll的引用只能在更改宏安全性(宏安全性(開發人員選項卡) - 宏設置 - 信任訪問VBA項目對象模型)後才能添加。此外,路徑可能在系統32以及取決於Windows版本 – sjd

0

的SendKeys是我的解決方案。

myfile = "C:\Users\User\Downloads\myfile.xls" 
checkmyfile = Dir(myfile, vbArchive) 

Do While checkmyfile = "" 
    On Error Resume Next 
    checkmyfile = Dir(myfile , vbArchive) 
    If checkmyfile = "myfile.xls" Then Exit Do 
    AppActivate "Title - Internet Explorer" 
    SendKeys "%(g)" 
    Application.Wait Now + TimeValue("0:0:1") 
Loop 
相關問題