How To Find Certain Files In VBA

Someone wanted to loop through the files in over 100 directories and find a list of their powerpoint files.

This code will loop a specific directory and check if a file with a “pptx” extension exists in that directory. If it does, a message box will be displayed. Instead of the message box, the file’s path could be inserted into a database table called “tblPPTFiles”.

Private Sub FindPPTX_Files()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim intCounter As Integer
    Dim strFolder As String
    Dim objSubFolder As Object
    Dim strInsSQL As String
    Dim varFile As Variant
  
    'CurrentDb.Execute ("DELETE FROM tblPPTFiles")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'root directory
    'strFolder = "S:\Share\OP_PrimDev\OBO_RMT\Companies\Caza Petroleum\Igloo 19 State\"
    strFolder = "C:\Users\erikloebl\Documents\access_references\a\"
    
    Set objFolder = objFSO.GetFolder(strFolder)
        
    For Each objSubFolder In objFolder.subFolders

       intCounter = 0

        Debug.Print " >>>> Subfolder: " & objSubFolder
        For Each varFile In objSubFolder.files
            Debug.Print varFile
            If Right(varFile, 4) = "pptx" Then
                'strInsSQL = "INSERT INTO tblPPTFiles (folder_name,folder,items) VALUES ('" & objFolder.Name & "','" & objSubFolder.Name & "','" & intCounter & "')"
                'CurrentDb.Execute strInsSQL
                MsgBox varFile
            End If
        
        Next varFile
        
    Next objSubFolder

    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

End Sub



By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

What is the purpose of the Me keyword in Access VBA?

What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]

How To Exit A VBA Loop

Hi, there are times when you need to exit a loop after a certain condition has been met. How do you exit function in VBA? In the following example, you are going to see how to exit a function in VBA: Sub StartNumbers() Dim intNumber As Integer intNumber = ExitTest ‘the number is going to […]

How To Do A VBA CSV Import For One To Multiple Files

So you want to be able to import your csv for 1 to multiple files. Here’s how you do it. 1. up a form with a button. 2. After you click the button, you will get a File Dialog Box that will show you the csv files to choose from: Here is the code behind […]

How To Have Access Export A Table To An Excel XLSX In VBA

This was regarding a question I received from this blog: Hello, I am hoping to find some help. I have an Access DB that I use to collect data. I have a number of different reporting features. I would like to add the option for my user to export to excel. When I do this […]

Previous Post

MS Access FilterOn Error (Solution)

Next Post

How To Identify The Last Row Worked On (VBA Variable Scope)