Adobe Reader could not open pdf because it is either not a supported file type or because the file has been damaged
Option Explicit
#If VBA7 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 LongPtr
#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
Sub DowloadSingleFile()
Dim FileURL As String
Dim DestinationFile As String
FileURL = "https://expo.expeditors.com/expotr/expotr?action=com.expd.webapp.tracking.action.document.DocumentImageDownloadWebAction&Type=Image.Get&xref=IVP9OABdjwlQyfAP4d8VNzKeBtYse1TlCWJmb5gzyMxbwbgxBLP19w%3D%3D"
DestinationFile = "C:\VBA\4750422330.pdf"
If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "file download started"
Else
Debug.Print "file download not started"
End If
End Sub
ASKER
Option Explicit
#If VBA7 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 LongPtr
#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 Sub Login()
Dim ie As New InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Links As MSHTML.IHTMLElementCollection
Dim Docs As MSHTML.IHTMLElement
Dim Link As MSHTML.IHTMLElement
Dim FileURL As String
Dim DestinationFile As String
'Dim ie As New InternetExplorer 'InternetExplorerMedium
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .getElementById("j_username")
.Focus
.Value = "bom-sumand"
End With
With .getElementById("j_password")
.Focus
.Value = "******"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
End With
With ie
.navigate "https://expo.expeditors.com/expotr/expotr?action=com.expd.webapp.tracking.action.searches.SearchWebAction&SearchType=advancedshipment.BaseAdvancedShipmentSearch&reference=4750422330#Document_Images"
.Visible = True
While .Busy Or .readyState < 4: DoEvents: Wend
Application.Wait (Now + TimeValue("0:00:10"))
Set HTMLDoc = ie.document
Set Docs = HTMLDoc.getElementsByClassName("sorting_1")(4)
Set Links = Docs.getElementsByTagName("a")
Debug.Print Links.Length
For Each Link In Links
If Link.innerText = "Commercial Invoice" Then
Debug.Print Link.innerText, Link.getAttribute("href")
FileURL = Link.getAttribute("href")
MsgBox FileURL
End If
Next Link
End With
DestinationFile = "C:\VBA\4750422330.PDF"
If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "file download started"
Else
Debug.Print "file download not started"
End If
End Sub
ASKER
<a href="/expotr/expotr?action=com.expd.webapp.tracking.action.document.DocumentImageDownloadWebAction&Type=Image.Get&xref=iwJBOhDL0VuA%2BQ21J9Sd9rdyZB2f3grZmFQyVhweT2UfCmJZ7l6a0g%3D%3D" target="_blank">Commercial Invoice</a>
Option Explicit
#If VBA7 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 LongPtr
#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 Sub Login()
Dim ie As New InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Links As MSHTML.IHTMLElementCollection
Dim Docs As MSHTML.IHTMLElement
Dim Link As MSHTML.IHTMLElement
Dim FileURL As String
Dim DestinationFile As String
'Dim ie As New InternetExplorer 'InternetExplorerMedium
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .getElementById("j_username")
.Focus
.Value = "bom-sumand"
End With
With .getElementById("j_password")
.Focus
.Value = "****"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
'End With
'With ie
.navigate "https://expo.expeditors.com/expotr/expotr?action=com.expd.webapp.tracking.action.searches.SearchWebAction&SearchType=advancedshipment.BaseAdvancedShipmentSearch&reference=4750422330#Document_Images"
.Visible = True
While .Busy Or .readyState < 4: DoEvents: Wend
Application.Wait (Now + TimeValue("0:00:10"))
Set HTMLDoc = ie.document
Set Docs = HTMLDoc.getElementsByClassName("sorting_1")(4)
Set Links = Docs.getElementsByTagName("a")
Debug.Print Links.Length
For Each Link In Links
If Link.innerText = "Commercial Invoice" Then
Debug.Print Link.innerText, Link.getAttribute("href")
FileURL = Link.getAttribute("href")
MsgBox FileURL
End If
Next Link
DestinationFile = "C:\VBA\4750422330.PDF"
If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "file download started"
Else
Debug.Print "file download not started"
End If
End With
End Sub
ASKER
Visual Basic for Applications (VBA) enables building user-defined functions (UDFs), automating processes and accessing Windows API and other low-level functionality through dynamic-link libraries (DLLs). VBA is closely related to Visual Basic and uses the Visual Basic Runtime Library, but it can normally only run code within a host application rather than as a standalone program. It can, however, be used to control one application from another via OLE Automation. VBA is built into most Microsoft Office applications.
TRUSTED BY
For ANY login page, you need FAR FAR more code (probably 10-fold of your current code), as first you have to investigate the login procedure, including fully understanding the traffic to and from, with minute details like which cookies to send back etc etc etc.
Very very laborous work, including the need to totally rewrite it whenever they decide to slightly change the login procedure.