Delete PDF Files from Workbook Folder Based on File Name Length

rickincanada

Board Regular
Joined
Aug 31, 2010
Messages
61
I'm attempting to create a macro that runs during the Workbook_Open event which deletes all the PDF files from the directory the workbook is in where the file name has 53 characters before the ".PDF". Any help you could provide me with would be greatly appreciated!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi Rick,

Something like this?

Code:
    Dim objFSO As New FileSystemObject
    Dim fldTarget As Folder
    Dim fle As File
    
    '//// Ensure you set reference to Microsoft Scripting Runtime
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set fldTarget = objFSO.GetFolder(ThisWorkbook.Path)
    
    For Each fle In fldTarget.Files
        With fle
            If Len(.Name) = 57 And LCase(Right$(.Name, 3)) = "pdf" Then .Delete
        End With
    Next fle
 
Last edited:
Upvote 0
Hi Mike - thanks for your help! The only problem with this is that I have over 100 different workbook users therefore ensuring that Microsoft Scripting Runtime is referenced is not possible. Is there anyway to automate this?
 
Upvote 0
The Reference to Microsoft Scripting Runtime is made in the workbook you initiate this code from, once you've done it in the file in question it'll be done for everybody :)
 
Upvote 0

Forum statistics

Threads
1,215,527
Messages
6,125,337
Members
449,218
Latest member
Excel Master

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