Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to open a file using FileSystemObject, but when I'm trying to run it, the system does nothing. Doesn't show me Debug, No runtime error, doesn't compile... It just remains as it is. I have also checked the "MS Scripting Runtime" in References.

What I have tried:

VB
Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, myFile As File
Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")

For Each sf In f.SubFolders
    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files
            If myFile.Name Like "Done" Then
                MsgBox myFile.Name
                Exit For
            End If
        Next

        MsgBox "Else"
    Next
Next

End Sub
Posted
Updated 30-Oct-17 5:44am
v2
Comments
F-ES Sitecore 30-Oct-17 8:30am    
If you're running this in Excel then it's probably a security issue. To create FSO (or any COM object) in a scripting environment it needs to me configured as safe to do so in the registry. As FSO is a dangerous component it's not marked as safe.
CHill60 30-Oct-17 8:38am    
If it doesn't compile then what errors do you get? Address those first.
When you do run it you will find that you have not declared mySubFolder nor myFolder. The variable myFolder is not initialised (I suspect it was meant to be sf).
You will not get any output if your Desktop does not have any sub-folders and only then if those sub-folders have their own sub-folders.
If you are trying to recursively search your folder f then you should use a recursive routine - Google has many examples

You may have to write
VB
For Each mySubFolder In sf.SubFolders

instead.
(Edit: that's why I don't like languages which do not enforce the declaration of variables)
 
Share this answer
 
v2
Further to phil.o's solution, here is a working solution that will crawl through all sub-folders of a starting folder and list all the files. It doesn't do exactly what you need but it does demonstrate how to recursively call a function.
VB
Option Explicit
Sub fsoobject()
    Dim fso As New FileSystemObject, f As folder
    Set f = fso.GetFolder("C:\Temp")
    Debug.Print "Start..."
    recurs f
    Debug.Print "...End"
End Sub
Sub recurs(folder As folder)
    Dim subFolder As folder
    For Each subFolder In folder.SubFolders
        recurs subFolder
    Next
    Dim myFile As File
    For Each myFile In folder.Files
        Debug.Print folder.Name & " " & myFile.Name
    Next
End Sub
Note the use of Option Explicit - I strongly advise you to always include that line in your VBA code. You can set that up as a default setting in the VBE.

As per my comment, if your code doesn't compile then you have to address those issues first. That's why I don't like languages that are interpreted (like VBA) as you can actually execute faulty code and only know about the problem when it crashes.

Finally, learn to use breakpoints and stepping through code with the debugger - it would have been quite clear what the problems were.
 
Share this answer
 
Comments
F-ES Sitecore 30-Oct-17 8:58am    
VBA isn't interpreted, VBScript is but VBA isn't :)
CHill60 30-Oct-17 9:07am    
VBA is only "compiled" to P-Code and then that is "interpreted". It's not taken as far as machine code or to an executable. The point I was making that you can have glaring syntactical errors in code yet still "run" the application. The errors are only highlighted when you hit the specific function or sub containing the error(s) - assuming your testing is robust enough to cover all code paths. This is in comparison to the more classically "compiled" languages where you cannot proceed to "running" your code until "compilation" is complete and successful.
F-ES Sitecore 30-Oct-17 9:21am    
That's like saying .net is only "compiled" to CLR and that is "interpreted". Technically VBA is a compiled language the same way VB proper was. The reason you can make mistakes with variable types etc as you can with an interpreted language is because it supports late-binding and "variant" types, but it is still a compiled language.
VB
Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, myFile As File
Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")

'For Each sf In f.SubFolders
'    For Each mySubFolder In sf.SubFolders
'        For Each myFile In mySubFolder.Files
'            If myFile.Name Like "Done" Then
'                MsgBox myFile.Name
'                Exit For
'            End If
'        Next
'
'        MsgBox "Else"
'    Next
'Next

    Dim File
    For Each File In f.Files
        If File.Name = "Done.PNG" Then
            Call Shell("Explorer.exe """ & File.Path & """", vbNormalFocus)
            Exit For
        End If
    Next

End Sub



Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, myFile As File
Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")

'For Each sf In f.SubFolders
' For Each mySubFolder In sf.SubFolders
' For Each myFile In mySubFolder.Files
' If myFile.Name Like "Done" Then
' MsgBox myFile.Name
' Exit For
' End If
' Next
'
' MsgBox "Else"
' Next
'Next

Dim File
For Each File In f.Files
If File.Name = "Done.PNG" Then
Call Shell("Explorer.exe """ & File.Path & """", vbNormalFocus)
Exit For
End If
Next

End Sub
Well, I searched a bit, and found this keyword "Shell" to open a file. On applying this code, now it executes successfully...
 
Share this answer
 
Comments
CHill60 30-Oct-17 16:56pm    
If you were trying to open the file then you should have made it clear that that was the bit that was the issue with the code you had. You made no attempt to open the files in your code snippet!
The more clarity you use with your question the better the solutions you will be offered
JayyMehta 31-Oct-17 5:19am    
I'm Sorry you didn't understand, but I have already mentioned in the question that "I'm trying to open a file..."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900