VBA to iterate through hundreds of PDFs in a directory

Priebe

New Member
Joined
Oct 6, 2015
Messages
3
Hello, I have a pretty urgent need for work. In a nutshell, I will have hundreds (possibly thousands) of PDFs in a local directory. Each PDF will be ~20 pages. I need to programmatically identify all PDFs that do not have a specific text string in them. Ideally, the output would be a list of the PDF filenames in a column of a single sheet in the workbook, one filename per row. I have looked around but so far have not found anything that I can adapt to accomplish this. Help is much appreciated!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Have assumed that the files to be listed do not have a specific text string in the file name.
Adapted from here : VBA Loop Through PDF Files in a Folder or Directory | VBAF1
VBA Code:
'VBA Loop Through .pdf files in a given Folder
Sub List_PDF_Files_WO_String()
    
    'Variable Declaration
    Dim sFilePath As String
    Dim sFileName As String
    Dim str As String
    
    'Specify File Path
    sFilePath = "D:\Test"
    
    'Specify string to look for
    str = "Some String"
    
    'Check for back slash
    If Right(sFilePath, 1) <> "\" Then
        sFilePath = sFilePath & "\"
    End If
        
    sFileName = Dir(sFilePath & "*.pdf")
    
    Do While Len(sFileName) > 0
        If Right(sFileName, 3) = "pdf" Then
            If InStr(sFileName, str) = 0 Then _
                ActiveSheet.Cells(Rows.Count, "A").End(3)(2) = sFileName
        End If
        'Set the fileName to the next available file
        sFileName = Dir
    Loop
   
End Sub
 
Upvote 0
Have assumed that the files to be listed do not have a specific text string in the file name.
Adapted from here : VBA Loop Through PDF Files in a Folder or Directory | VBAF1
VBA Code:
'VBA Loop Through .pdf files in a given Folder
Sub List_PDF_Files_WO_String()
   
    'Variable Declaration
    Dim sFilePath As String
    Dim sFileName As String
    Dim str As String
   
    'Specify File Path
    sFilePath = "D:\Test"
   
    'Specify string to look for
    str = "Some String"
   
    'Check for back slash
    If Right(sFilePath, 1) <> "\" Then
        sFilePath = sFilePath & "\"
    End If
       
    sFileName = Dir(sFilePath & "*.pdf")
   
    Do While Len(sFileName) > 0
        If Right(sFileName, 3) = "pdf" Then
            If InStr(sFileName, str) = 0 Then _
                ActiveSheet.Cells(Rows.Count, "A").End(3)(2) = sFileName
        End If
        'Set the fileName to the next available file
        sFileName = Dir
    Loop
  
End Sub
Actually needed to search within the PDF documents themselves, not the filename. But, we were able to come up with another solution.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

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