[英]Download PDF from current Internet Explorer browser session with Excel VBA?
我希望從我們的公司網站自動下載多個 PDF。 該站點僅適用於我們的內部公司網絡/VPN,並且需要身份驗證(並且也僅適用於 https)。
我已經研究過通過 VBA/python 登錄,但遇到了麻煩。 我想是由於我們公司網絡設置和訪問該站點的限制的某種組合。
我認為最簡單的方法是使用現有的瀏覽器會話來下載文件,而不是擔心所有的身份驗證和網絡問題?
我改編了我在網上找到的 VBA 代碼來識別和設置一個現有的、經過身份驗證的 IE 窗口的變量,並導航到我們公司網站上的 PDF(見下文)。
從那里,我如何從現有的瀏覽器會話中自動保存 PDF 頁面? 我在網上看到的用於在 IE 中保存文件的幾種方法似乎不起作用。 如果這通過 python 更容易,我也對此持開放態度。 謝謝!
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_title Like "XYZ" & "*" Then 'compare to find if the desired web page is already open
Set ie = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 then
msgbox("A matching webpage was NOT found")
Else
msgbox("A matching webpage was found")
ie.navigate("https://corpwebsite.com/abcdef.pdf")
End If
試試 URLMon 直接從 URL 下載? 假設您已經處理了任何身份驗證問題。
Option Explicit
#If VBA7 And Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr _
) As Long
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long _
) As Long
#End If
Public Const BINDF_GETNEWESTVERSION As Long = &H10
Public Const folderName As String = "C:\Users\User\Desktop\abcdef.pdf" '<=Change as required
Public Sub downloadPDF()
'Authentication code first. Maybe in a different sub.
Dim ret As Long
ret = URLDownloadToFile(0, "https://corpwebsite.com/abcdef.pdf", folderName, BINDF_GETNEWESTVERSION, 0)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.