I am attempting to step through the files in a directory using an Excel VBA macro. I have set up a very simple stub:
Sub ListAllFiles()
Dim MyFile As String, MyPath As String
MyPath = "C:\Users\user01\Documents\Word Files\"
MyFile = Dir(MyPath, vbNormal) 'This returns a valid file name
Do While MyFile <> ""
Debug.Print MyFile
MyFile = Dir("")
'This returns MyFile = ""
Loop
End Sub
The first MyFile assignment returns a valid file name (in the MyPath directory). According to MS documentation the subsequent DIR("") should return the next file and allow the code to iterate through the file names in the directory. It doesn't work. It returns MyFile = "" and exits the loop.
I am running Windows 7 Ultimate (x64) and Excel 2013. Am I doing something wrong? Is there a bug? Help!
Alex3764