【问题标题】:VBA - choose save as from the frame notification bar of internet explorerVBA - 从 Internet Explorer 的框架通知栏中选择另存为
【发布时间】:2017-05-20 20:52:29
【问题描述】:

我正在尝试通过 Internet Explorer 的 框架通知栏 下载保存为的文件。 但是经过大量搜索后,我只找到了在框架通知栏上单击save 的解决方案。 到目前为止,我一直在尝试在示例网站上另存为文件:

http://www.tvsubtitles.net/subtitle-114117.html

使用以下代码:

' Add referenses
' Microsoft Internet Controls
' Microsoft HTML Object Library
' UIAutomationClient (copy file from C:\Windows\System32\UIAutomationCore.dll to Documents Folder)

#If VBA7 Then
    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
    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 downloadfilefromeie()

    Dim subpage As InternetExplorer
    Dim objpage As HTMLDocument
    Dim o As CUIAutomation
    Dim h As LongPtr
    Dim fnb As LongPtr
    Dim e As IUIAutomationElement
    Dim iCnd As IUIAutomationCondition
    Dim Button As IUIAutomationElement
    Dim InvokePattern As IUIAutomationInvokePattern
    Dim strBuff As String
    Dim ButCap As String

    Set objshell = CreateObject("Shell.Application")
    Set objallwindows = objshell.Windows
    Set subpage = New InternetExplorer
    For Each ow In objallwindows
        'MsgBox ow
        If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then
            'MsgBox ow.Hwnd & "  " & ow & "   " & ow.locationURL
            If (InStr(1, ow.locationURL, "tvsub", vbTextCompare)) Then
                Set subpage = ow
            End If
        End If
    Next
    Set objpage = New HTMLDocument
    If subpage Is Nothing Then
    Else
        Set objpage = subpage.Document
        'Debug.Print objpage
        'objpage.getElementById("content").Click
        Set dl = objpage.getElementsbyclassname("subtable")
        Set dltable = dl(0).FirstChild.ChildNodes
        Set dlrow = dltable(10).getElementsByTagName("a")(2)
        dlrow.Click
        While objpage.ReadyState <> "complete"
            DoEvents
        Wend
    End If
    Application.Wait (Now() + TimeValue("0:00:05"))
    Set o = New CUIAutomation
    h = subpage.Hwnd
    fnb = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If fnb = 0 Then Exit Sub
    'Debug.Print "type of fnb is " & TypeName(fnb)
    Set e = o.ElementFromHandle(ByVal fnb)
    'Debug.Print "type of e is " & TypeName(e)
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    'Debug.Print "type of Button is " & TypeName(Button)
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    'Debug.Print "type of InvokePattern is " & TypeName(InvokePattern)
    InvokePattern.Invoke

End Sub

我尝试将"Save" 更改为"Save as",但它不起作用。我的猜测是,在访问另存为按钮之前,我需要先以某种方式单击拆分按钮上的箭头,但我没有成功。 如果有人可以提供解决方案,将不胜感激。

【问题讨论】:

  • 对代码的一些提示: 1) Set subpage = New InternetExplorerSet objpage = New HTMLDocument 行是不必要的。 2) 将Set objallwindows = objshell.Windows ... For Each ow In objallwindows ... Next 块放入Do ... Loop,在Set subpage = ow 之后添加Exit Do 行,还在某处添加DoEvents。 3) 那么If subpage Is Nothing Then也不需要了。
  • 您能否提供更多详细信息,您尝试下载哪些数据以及从哪里下载?可能有另一种解决方案可以找到,更可靠,根本不需要 IE。

标签: excel vba internet-explorer web-scraping


【解决方案1】:

我尝试通过链接http://www.tvsubtitles.net/download-114117.html 下载一个文件,该链接可以在网页http://www.tvsubtitles.net/subtitle-114117.html 上找到,它对我有用,这里是代码:

Sub Test_download_tvsubtitles_net()

    DownloadFile "http://www.tvsubtitles.net/download-114117.html", ThisWorkbook.Path & "\download-114117.zip"

End Sub

Sub DownloadFile(sUrl, sPath)

    Dim aBody

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sUrl, False
        .Send
        aBody = .responseBody
    End With
    With CreateObject("ADODB.Stream")
        .Type = 1 ' adTypeBinary
        .Open
        .Write aBody
        .SaveToFile sPath, 2 ' adSaveCreateOverWrite
        .Close
    End With

End Sub

【讨论】:

  • 感谢 omegastripes!
  • 感谢 omegastripes!问题是我需要在工作中应用它,并且我需要在工作中下载的文件需要通过框架通知栏下载(因为基本上我不是点击下载链接而是下载内容页面作为带有自定义分隔符等的 .txt 文件)您认为这可能与您的方法有关吗?再次感谢。
  • @bangbangjim 不太清楚为什么需要通过框架通知栏下载,您写道您没有单击下载链接,但实际上这正是您的代码所做的dlrow.Click。我的问题是使用您的代码下载的文件(我已经检查过 - 它工作正常)和上面的代码有什么区别?
  • 我这样做了 dlrow.click 只是为了按下下载链接,这样会弹出一个框架通知栏,我可以在家工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
相关资源
最近更新 更多