-2

I have a list of pdfs which are located in one folder and subfolders within that folder. I would like to be able to have a macro that goes down the list and prints each of the pdf's after finding them in the folders.

The list of pdf names in the excel sheet start on B3 and go down. The look like "10028844" while the pdf's are saved with the same name "10028844.pdf".

I've looked all around and have found many examples on how to look through folders for ALL files in the folder/subfolder but none that look for specific files or a list of files. Any help is appreciated.

I've found some articles that helped with some code by adding the files to a collection but when I run this macro there is nothing in the collection. Does anyone see where this is going wrong?

Sub GetFiles(StartFolder As String, Pattern As String, _
         DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
    colFiles.Add StartFolder & f
    f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
    If sf <> "." And sf <> ".." Then
        If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
                subF.Add StartFolder & sf
        End If
    End If
    sf = Dir()
Loop

For Each s In subF
    GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub

.

Sub BatchPrint()

Dim colFiles As New Collection
Dim CustRow, LastRow As Long

LastRow = Sheet1.Range("B9999").End(xlUp).Row

With Sheet1

For CustRow = 3 To LastRow

GetFiles "C:\Users\Desktop\Test\", "B" & CustRow & ".pdf", True, colFiles
If colFiles.Count > 0 Then
'work with found files
End If

Next CustRow

End With

Dim i As Long
For i = 1 To colFiles.Count
Debug.Print colFiles(i)
Next i

End Sub
JoshL
  • 164
  • 1
  • 12

1 Answers1

0

After some work I finally got this to work. Code below. Choose what happens to the collection by changing the line Debug.Print colFiles(i).

Sub GetFiles(StartFolder As String, Pattern As String, _
         DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
    colFiles.Add StartFolder & f
    f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
    If sf <> "." And sf <> ".." Then
        If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
                subF.Add StartFolder & sf
        End If
    End If
    sf = Dir()
Loop

For Each s In subF
    GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub

.

Sub BatchPrint()

Dim colFiles As New Collection
Dim CustRow, LastRow As Long

Set colFiles = New Collection

LastRow = Sheet1.Range("B9999").End(xlUp).Row

With Sheet1

For CustRow = 3 To LastRow

GetFiles "C:\Users\Desktop\Test\", Sheet1.Range("B" & CustRow) & ".pdf", True, colFiles

Next CustRow

End With

Dim i As Long
For i = 1 To colFiles.Count
    Debug.Print colFiles(i)
Next i

Set colFiles = Nothing

End Sub
JoshL
  • 164
  • 1
  • 12