帧通知栏中的文件名

时间:2018-03-06 20:07:19

标签: vba internet-explorer

有没有人知道是否有方法从"框架通知栏中提取文件名"在IE11中使用hwnd?下面的代码很好,但我试图找到一个更好的方法来查找文件(动态命名),然后从下载文件夹中提取最新的文件。提前致谢!

Sub SaveFile(IE As InternetExplorerMedium)
Dim o As IUIAutomation, buffer As String, length As Long, result As Long, e      As IUIAutomationElement, h As Long
Set o = New CUIAutomation
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, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
End Sub

1 个答案:

答案 0 :(得分:1)

我知道这是一个古老的问题,但是由于没有答案,而且我找到了解决问题的方法,因此我认为分享它很好。

您要查找的元素是Notification bar Text

我的解决方案基于本教程:

https://www.mrexcel.com/board/threads/using-uiautomationclient-to-automate-the-save-as-file-download-in-ie11.1086615/

添加这些声明:

        Dim iCnd2 As IUIAutomationCondition
        Dim iCnd3 As IUIAutomationCondition
        Dim iCnd2and3 As IUIAutomationCondition
        Dim NotificationBarText As IUIAutomationElement

Set e = o.ElementFromHandle(ByVal h)之后添加以下内容:

        Set iCnd2 = o.CreatePropertyCondition(UIA_NamePropertyId, "Notification bar Text")
        Set iCnd3 = o.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_TextControlTypeId)
        Set iCnd2and3 = o.CreateAndCondition(iCnd2, iCnd3)
        Set NotificationBarText = e.FindFirst(TreeScope_Descendants, iCnd2and3)
        NotificationBarTextString = NotificationBarText.GetCurrentPropertyValue(UIA_ValueValuePropertyId)
        baseUrl = Mid(Mid(IE.LocationURL, InStr(IE.LocationURL, "//") + 2), 1, InStr(Mid(IE.LocationURL, InStr(IE.LocationURL, "//") + 2), "/") - 1)
        FileName = Replace(Replace(NotificationBarTextString, "Do you want to open or save ", ""), " from " & baseUrl & "?", "")
相关问题