Forum Discussion

hrh_dash's avatar
hrh_dash
Iron Contributor
Oct 17, 2022

Macro execute to loop all word doc files in a folder

How do i amend the code below so that the macro could select all word doc files in the folder?

There are no errors but when the code is executed line by line, the macro did not run through the Do While loop.

    Dim path        As String
    Dim oDoc        As Object
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    path = "C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile"
    
    myFile = Dir(path & "*.*")
    
    Do While myFile <> ""
        
        Set oDoc = documents.Open(filename:=path & myFile)
        
        Call extractdatafromwordtoexcel
        Call findcopyandpaste
        
        wordDoc.Close
        myFile = Dir
        
    Loop
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
End Sub

Appreciate the assist in advance..

 

  • hrh_dash 

    Also, to be on the safe side, I'd explicitly loop through Word documents, so that you don't get an error if the folder contains another type of file:

        path = "C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile\"
        
        myFile = Dir(path & "*.doc*")
    
  • hrh_dash 

    Also, to be on the safe side, I'd explicitly loop through Word documents, so that you don't get an error if the folder contains another type of file:

        path = "C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile\"
        
        myFile = Dir(path & "*.doc*")
    
    • hrh_dash's avatar
      hrh_dash
      Iron Contributor

      HansVogelaar , 

      , if i would like to add an additional file type such as docx; would the following be correct?

      currently, nothing is being populated when the code is executed.

       

          myFile = Dir(path & "*.docx*;*.doc*")
      • hrh_dash 

         

        "*.doc*" includes .doc, .docx and .docm

        If the code doesn't process any documents, there must be something wrong with the path.

        As JMB17 asked: "Is "Bizfile" the name of a folder, or is it the first part of the filename(s)?"

  • JMB17's avatar
    JMB17
    Bronze Contributor
    Is "Bizfile" the name of a folder, or is it the first part of the filename(s)? If it's a folder, then you need a "\" to separate "Bizfile" and "*.*".

    C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile\*.*

Resources