1

Am I missing a reference object? Trying to retrieve a list of files in a directory and have them show in a sheet.

Also, would it be possible to get the directory location from a specific cell in the file?

Sub LoopThroughFiles()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\VBA Folder")

For Each oFile In oFolder.Files

    Cells(i + 1, 1) = oFile.Name

    i = i + 1

Next oFile

End Sub

Here:

Set oFSO = CreateObject("Scripting.FileSystemObject")

Runtime error 429 on a Windows machine.

11
  • Your code works fine for me. Commented Nov 22, 2019 at 17:55
  • @JohnnyMopp lol Thanks! That is why I am thinking maybe I'm missing a reference or DLL or something. Commented Nov 22, 2019 at 18:01
  • As far as I know, Scripting.FileSystemObject should be available on all Windows machines, so I don't know what you are missing. I tested by copy-and-pasting into a new spreadsheet and it worked oob. Maybe you need elevated permissions?? Commented Nov 22, 2019 at 18:06
  • @JohnnyMopp I will check that. Maybe open the file as admin? Commented Nov 22, 2019 at 18:08
  • 1
    I bet it's removing the Hungarian Notation that fixed it! ;-) Commented Nov 22, 2019 at 19:10

1 Answer 1

1

There's little to no reason at all to late-bind the Scripting library - it's present (same identical version) on every single Windows box out there, and won't work on a Mac whether you late-bind it or not.

Tools/References, add the "Microsoft Scripting Runtime" library to your project, and then declare the actual data types for your object variables; you'll get compile-time validation and intellisense/autocompletion (good-bye error 438!) for all member calls.

Early binding is your friend. Declare As Scrpiting.FileSystemObject, and then just New up the object instead of hitting the registry to resolve the "Scripting.FileSystemObject" ProgID with CreateObject:

Set fso = New Scripting.FileSystemObject

Or have a With block whithold the object reference, and then you don't even need a local variable for it:

With New Scripting.FileSystemObject
    '....
End With
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.