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

How to open an URL in a browser using VBA?

正在尝试打开带有引用的 PDF(网站),而只能用 parents 页面上的 link-click 打开。

通过使用

 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 [输入,可选] Headers [in, 可选])

尝试过

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

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

我相信你给Headers参数提供了错误的Referer URL(而且格式也不对,除了Referer 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