Consulting

Results 1 to 3 of 3

Thread: Function to list pdf files within a folder

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location

    Function to list pdf files within a folder

    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!
    Edmond

  2. #2
    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.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location
    I see. Thank you for your explanation!
    Edmond

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •