Reputation: 63
I am looking to automate the downloading of multiple PDFs from our corporate website. This site only works over our internal corporate network/VPN and requires authentication (and is also https only).
I've looked into logging in via VBA/python but have had troubles. I imagine due to some combination of our corporate network set up and restrictions on accessing the site.
I think the easiest thing would be to just use an existing browser session to download the files, rather than worry about all the authentication and network issues?
I adapted VBA code I found online to identify and set a variable to an existing, authenticated IE window and navigate to a PDF on our corporate website (see below).
From there, how can I automatically save the PDF page from the existing browser session? The couple ways I saw online for saving files in IE dont seem to work. If this is easier through python I am also open to that. Thanks!
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
Upvotes: 3
Views: 1711
Reputation: 84465
Try URLMon to download direct from URL? Assume you have handled any authentication issues.
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
Upvotes: 3