How can I automate the saving of a PDF file after it is opened via an Excel hyperlink? I open the pdf file via hyperlink and then save to a specific subdirectory after naming the file. I need to do this 100s of times within each Excel.
& ".pdf"
in the code.Public Sub Copy_Hyperlinked_Files()
Dim destFolder As String
Dim lastRow As Long, r As Long
destFolder = "C:\files\pdfs\"
If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
With ActiveSheet
lastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
For r = 2 To lastRow
FileCopy .Cells(r, "T").Hyperlinks(1).Address, destFolder & .Cells(r, "E").Value & ".pdf"
Next
End With
End Sub
Which line? Click Debug on the error message and the errant line is highlighted. Does the active sheet contain hyperlinks in column T starting at T2? If not, that would cause the error.Do i need to do something different in the MACRO? Currently I get a "SUBSCRIPT OUT OF RANGE" Error when I run it.
#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon.dll" Alias "URLDownloadToFileA" _
(ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As LongPtr) As Long
#Else
Private Declare Function URLDownloadToFile Lib "urlmon.dll" 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
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Public Sub Copy_Hyperlinked_Files()
Dim destFolder As String
Dim lastRow As Long, r As Long
destFolder = "C:\files\pdfs\"
If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
With ActiveSheet
lastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
For r = 2 To lastRow
DownloadFile .Cells(r, "T").Hyperlinks(1).Address, destFolder & .Cells(r, "E").Value & ".pdf"
Next
End With
End Sub
Private Function DownloadFile(url As String, localFilename As String) As Boolean
Dim retVal As Long
retVal = URLDownloadToFile(0, url, localFilename, BINDF_GETNEWESTVERSION, 0)
If retVal = 0 Then DownloadFile = True Else DownloadFile = False
End Function