PDA

View Full Version : [SOLVED] Function to list pdf files within a folder



Edmond
03-11-2019, 11:39 AM
Hi everyone,

I have found this code, perfectly working, to do as the title indicates it:

Sub NameChanging()

Dim fls, f
Set fls = GetFiles("C:\Users\Test\", "*.pdf*")
For Each f In fls
Debug.Print f
Next f


End Sub


Function GetFiles(path As String, Optional pattern As String = "") As Collection
Dim rv As New Collection, f
If Right(path, 1) <> "\" Then path = path & "\"
f = Dir(path & pattern)
Do While Len(f) > 0
rv.Add path & f
f = Dir() 'no parameter
Loop
Set GetFiles = rv
End Function

None the less, I have troubles finding out how it does work without "dimensioning" the variables f and fls.
Could someone explain me briefly what's the purpose of not mentioning the dimension? And a some explanation on the typo? "Dim Name As Var, f?"

Thank you!

Jan Karel Pieterse
03-12-2019, 11:57 PM
rv is declared as a collection. Each filename is a string which is subsequently added to the collection using the "rv.Add path & f" command. Just highlight the word COllection in the code and press F1 for help on collections.

Edmond
03-14-2019, 11:39 AM
I see. Thank you for your explanation!