Ich versuche, Excel-Dateien aus dem Internet heruntergeladen und kopieren Sie die Daten in eine andere Excel-Datei während VBA läuft mit ihren Befehlen.(VBA Commands Priority) Wie öffne ich zuerst Excel-Dateien vom Internet Explorer während VBA-Läufen?
Aber der Befehl, der die Excel-Datei öffnet, wird nach den Enden von VBA-Code unabhängig von ihrer Position ausgeführt.
Zum Beispiel zeigt der folgende Code den gesamten Prozess des Herunterladens und Öffnens der Excel-Datei von einer Site. Aber bei der "open_excel" -Funktion tritt "InvokePattern.Invoke" tatsächlich nach der Ausführung aller vba-Codes auf.
Wie führe ich das zuerst aus? Kann ich diesem Befehl Priorität einräumen?
Oder wie man auf "InvokePattern.Invoke" bis zum Ende warten? (Ich versuche, Wartezeit oder Arten von Zeitmanipulation, die nicht funktioniert)
Sub crawler_main() ' this is main function
.....
Call ieopen(ie, url_futures) 'internet explorer is opened with some url
Call click_excel(ie)
Call open_excel(ie)
Call copy_data(wbname)
.....
End Sub
Sub ieopen(ie As InternetExplorer, url As String) ' open ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
Application.Wait (Now + TimeValue("00:00:05"))
End Sub
Sub click_excel(ie As InternetExplorer) 'download excel
Dim inquiry As Object
Set inquiry = ie.document.getElementsByClassName("btn-board btn-board-search")(0)
inquiry.Click
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
'Application.Wait (Now + TimeValue("00:00:05"))
Dim Buttons_Excel As Object
Dim Button As Object
Set Buttons_Excel = ie.document.getElementsByTagName("button")
For Each Button In Buttons_Excel
If Button.innerHTML = "Excel" Then
Button.FireEvent ("onclick")
Exit For
End If
Next
End Sub
Sub open_excel(ie As InternetExplorer) 'click open in dialog open/save
Dim e As IUIAutomationElement
Dim o As CUIAutomation
Set o = New CUIAutomation
Dim h As Long
h = ie.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, "열기")
Dim Button_Download As IUIAutomationElement
Set Button_Download = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button_Download.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
End Sub
Sub copy_data(wbname As String) 'copy data from recently opened file.
'But here is problematic since file is open after the execution of all vba codes
For Each wb In Application.Workbooks
If wb.Name Like "dat" & "*" Then
Set wb_data = Workbooks(wb.Name)
Exit For
End If
Next wb
............
End Sub
Danke für ein commnet, Aber URL ist zusammengebrochen, konnten Sie mir eine volle Adresse geben? – JonghoKim