(VBA命令优先)如何在vba运行期间从Internet Explorer首先打开excel文件?

我试图打开从互联网downloded excel文件,然后复制数据到另一个excel文件在vba运行与他们的命令。

但是打开excel文件的命令是在vba代码结束后执行的,而不pipe它们的位置。

例如,下面的代码显示了从站点下载和打开excel文件的整个过程。 但是在“open_excel”函数中,“InvokePattern.Invoke”实际上是在所有vba代码执行之后发生的。

我该如何执行这个呢? 我可以优先考虑这个命令吗?

或者如何等待“InvokePattern.Invoke”,直到完成? (我尝试等待时间或种类的时间操作不起作用)

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 

这可能是相关的: http : //msdn.microsoft.com/en-us/libr…87(VS.85).aspx

请注意,你可以使用更高的实时,但你将不得不设置权限这样做…代码:

 Sub SetPriority() Const ABOVE_NORMAL = 32768 Const HIGH = 128 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colProcesses = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = 'excel.exe'") For Each objProcess In colProcesses objProcess.SetPriority (HIGH) Next End Sub