无法使用 fso 打开文件

Unable to open file using fso

我正在尝试使用 FileSystemObject 打开一个文件,但是当我尝试 运行 它时,系统没有执行任何操作。不显示调试,没有 运行 时间错误,不编译。它只是保持原样。 我还检查了参考文献中的 "MS Scripting Runtime"。 下面是我为此使用的代码:

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
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

好吧,我搜索了一下,找到了这个关键词"Shell"来打开一个文件。应用此代码后,现在它已成功执行...

Sub fsoobject()
Dim fso As New FileSystemObject
dim f As Folder
dim  sf As Folder
dim mySubFolder as folder 
dim  myFile As File
 Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")
 dim found as boolean 'flag if found 
 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 mysubfolder.path & "\" & myFile.Name
                  found = true 'we found it
                  Exit For 'so stop
              End If
        Next myFile
        if found then exit for  'cascade exit
    Next MySubFolder
    if found then exit for 'and again
 next sf

 End Sub

这段代码对我有用。清晰简单:

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

For Each sf In f.SubFolders
    Set ssf = sf

    For Each myFile In ssf.Files
        If myFile.Name Like "Done" Then
            Debug.Print myFile.Name
            Exit For
        End If

    Debug.Print "Else:" & myFile.Name

    Next
Next

End Sub
  1. 设置子子文件夹(设置 ssf = sf)
  2. 我建议使用 Debug.Print 检查 VBE 中的循环