如何使用 VBA 在浏览器中打开 URL?

问题描述

尝试使用只能通过在父页面上单击链接打开的引用来打开 PDF(-网站)。

通过使用

 CreateObject(WinHttp.WinHttpRequest.5.1) 
.setRequestHeader "referer","https://...“ 

访问有效,但我需要在浏览器中打开页面才能查看 pdf。

找到这个:

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752094(v=vs.85)

语法:object.Navigate2(URL,Flags,TargetFrameName,PostData,Headers)

(PostData [输入,可选] 标题[输入,可选])

尝试过

 Dim IE As InternetExplorer  
 Set IE = New InternetExplorer 
    
   With IE
    .Navigate2
    https://main...,"https://referer..."

没有结果!有没有人有办法解决吗? (请只使用 VBA!谢谢)

解决方法

我相信您在 Headers 参数中提供了错误的引用 URL(并且格式也有误,除了引用 URL 之外,您还需要包含 Referer:),试试这个:

Private Sub Test()
    Dim oIE As InternetExplorer
    Set oIE = New InternetExplorer
        
    With oIE
        .Visible = True
        .navigate "https://www.zvg-portal.de/index.php?button=showAnhang&land_abk=ni&file_id=16396&zvg_id=6467",_
                    headers:="Referer: https://www.zvg-portal.de/index.php?button=showZvg&zvg_id=6467&land_abk=sh"
    End With
    oIE.Quit
    Set oIE = Nothing
End Sub 
,

支持 Raymond 非常方便。

如果你想走很长的路,你可以按如下方式与下拉菜单交互。生成结果后,单击指向 pdf 的链接。请注意,第一个下拉列表有一个 onchange 事件,它将 select 中的值作为参数。

Option Explicit

Public Sub ClickDownloads()
    Dim ie As SHDocVw.InternetExplorer,html As MSHTML.HTMLDocument

    Set ie = New SHDocVw.InternetExplorer: Set html = New MSHTML.HTMLDocument

    With ie
        .Visible = True
        .Navigate2 "https://www.zvg-portal.de/index.php?button=Termine suchen"
 
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    
        Dim evt As Object
        
        Set evt = .document.createEvent("HTMLEvents")
        evt.initEvent "onchange",True,False
        
        .document.querySelector("[value='ni']").Selected = True
        .document.parentWindow.execScript "updateAmtsgericht('ni');"
        .document.querySelector("[value='P2411']").Selected = True
        .document.querySelector("[type=submit]").Click
        
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
         
        Dim linkNodes As Object,i As Long
         
        Set linkNodes = .document.querySelectorAll("td:last-child a")
        
        For i = 0 To linkNodes.Length - 1
         
            linkNodes.Item(i).Click
            'Do something. Interact with save as dialogue to save. May also want to loop windows to close new tabls that were opened.
        Next
         
        Stop
 
        .Quit
    End With

End Sub