【发布时间】:2018-02-01 10:37:41
【问题描述】:
我正在尝试自动化一些通过 Internet Explorer 完成的任务,其中包括下载文件,然后将其复制到不同的目录并重命名。 我或多或少地成功找到了有关如何执行此操作的信息,代码正在运行,但它有例外,因此如果有人可以帮助我改进此代码,我将不胜感激。
我想做两件事:
- 插入一个循环,以便脚本等待某些元素出现,然后才会继续执行。我在this 页面上找到了一些东西,但是,我也想建立一个最长等待时间,就像那里建议的那样。
- 由于代码正在下载文件,它还应该等待下载完成,然后才能继续。目前我正在使用“等待”命令,但下载时间可能会有所不同,并且在这种情况下脚本将停止。我也找到了解决方案,等待“打开文件夹”按钮出现,但我不确定如何在我的代码中实现它。这是我找到的代码:Link
另外,也许还有另一种解决方案,不要将文件保存在默认下载位置,而是执行“另存为”,然后以这种方式定义目录和文件名?
提前谢谢你!
以下是我现在正在使用的源代码。例如,我正在使用带有示例文件下载的 Microsoft 页面。
Option Explicit
#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As LongPtr
#Else
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
#End If
Sub MyIEauto()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Set ieApp = New InternetExplorer
ieApp.Visible = True
ieApp.navigate "https://docs.microsoft.com/en-us/power-bi/sample-financial-download"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
ieApp.navigate "http://go.microsoft.com/fwlink/?LinkID=521962"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Dim AutomationObj As IUIAutomation
Dim WindowElement As IUIAutomationElement
Dim Button As IUIAutomationElement
Dim hWnd As LongPtr
Set AutomationObj = New CUIAutomation
Do While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:05"))
hWnd = ieApp.hWnd
hWnd = FindWindowEx(hWnd, 0, "Frame Notification Bar", vbNullString)
If hWnd = 0 Then Exit Sub
Set WindowElement = AutomationObj.ElementFromHandle(ByVal hWnd)
Dim iCnd As IUIAutomationCondition
Set iCnd = AutomationObj.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Set Button = WindowElement.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
Application.Wait (Now + TimeValue("0:00:05"))
FileCopy "C:\Users\Name\Downloads\Financial Sample.xlsx", "C:\Users\Name\Desktop\Financial Sample.xlsx"
Name "C:\Users\Name\Desktop\Financial Sample.xlsx" As "C:\Users\Name\Desktop\Hello.xlsx"
Application.Wait (Now + TimeValue("0:00:01"))
Dim KillFile As String
KillFile = "C:\Users\Name\Downloads\Financial Sample.xlsx"
If Len(Dir$(KillFile)) > 0 Then
SetAttr KillFile, vbNormal
Kill KillFile
End If
End Sub
【问题讨论】:
-
最终目标是简单地下载诸如
go.microsoft.com/fwlink/?LinkID=521962之类的文件,还是我缺少什么? -
好吧,当前代码完成了所有需要的工作,但我会将它用于不同的文件,等待命令 Application.Wait (Now + TimeValue("0:00:05")) 将并非每次都有效,例如,如果下载时间超过 5 秒。我想更改它以检查是否存在“打开文件夹”按钮,这意味着下载已完成。在此之前,脚本应该通过检查循环暂停。
-
我想做的另一个更改是检查是否存在特定元素,因此即使浏览器本身处于就绪状态,但页面尚未加载,我也可以使用该脚本。
-
我想我有一个更简单/替代的解决方案给你...(我稍后会作为答案发布。)
标签: vba internet-explorer download browser-automation ie-automation