Rename PDF file

keaveneydan

Board Regular
Joined
Apr 29, 2014
Messages
144
Hi

I thought I had a nice bit of code that will rename PDF files for me. It does rename my file accordingly however I get an error when I try to open them saying they are damaged and could not be repaired. The macro, or a variation of it, works perfectly for excel files of the same name.

Thanks


Code:
Sub RenamePDFFiles()
Application.DisplayAlerts = False
        Application.ScreenUpdating = False
Dim FilePath As String
Dim StopMacro As Boolean
Dim CurrentFile As String
Dim FileDate As String
Dim FSO As New FileSystemObject
Dim aFile As File
On Error GoTo ErrorHandler
    
    FilePath = "S:\Market_Funds\PORT Data\FTP download\"
    If Len(Dir(FilePath & "Renamed Files", vbDirectory)) = 0 Then
        MkDir (FilePath & "Renamed Files")
    End If
    Do Until StopMacro = True
        CurrentFile = Dir(FilePath & "*.pdf")
        If CurrentFile = "" Then
            MsgBox "All PDF Files renamed.", vbOKOnly + vbCritical, "No PDF Files Found"
            Exit Sub
        End If
            Workbooks.Open FILENAME:=FilePath & CurrentFile
            Set aFile = FSO.GetFile(FilePath & CurrentFile)
            'If Weekday(Now(), vbMonday) = 1 Then
            'FileDate = Format(aFile.DateLastModified - 3, "YYYYMMDD")
            'Else
            'FileDate = Format(aFile.DateLastModified - 1, "YYYYMMDD")
            'End If
            Set aFile = FSO.GetFile(FilePath & CurrentFile)
            If Weekday(aFile.DateLastModified, vbSaturday) = 1 Then
            FileDate = Format(aFile.DateLastModified - 1, "YYYYMMDD")
            ElseIf Weekday(aFile.DateLastModified, vbMonday) = 1 Then
            FileDate = Format(aFile.DateLastModified - 3, "YYYYMMDD")
            ElseIf Weekday(aFile.DateLastModified, vbSunday) = 1 Then
            FileDate = Format(aFile.DateLastModified - 2, "YYYYMMDD")
            Else
            FileDate = Format(aFile.DateLastModified - 1, "YYYYMMDD")
            End If
            ActiveWorkbook.SaveAs FILENAME:=FilePath & "Renamed Files\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 12) & " " & FileDate & (".pdf")
                Set aFile = Nothing
            ActiveWorkbook.Close
        Kill FilePath & CurrentFile
        Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    
ErrorHandler:
If Err.Number <> 0 Then
Msg = Str(Err.Number)
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Resume Next
    
    Application.DisplayAlerts = True
        Application.ScreenUpdating = True
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,215,506
Messages
6,125,194
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top