【问题标题】:Unable to open file using fso无法使用 fso 打开文件
【发布时间】:2017-10-30 12:02:20
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • sf替换myFolder
  • 仍然没有发生 :(
  • 唯一的 Like "Done" 是“完成” - 也许你需要通配符。
  • 如SJR所说,wildcardLike "*Done*"
  • @JayyM 不应该 For Each mySubFolder In myFolder.SubFoldersFor Each mySubFolder In sf.SubFolders 吗?

标签: excel filesystemobject fso vba


【解决方案1】:
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

【讨论】:

    【解决方案2】:
    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”来打开一个文件。应用此代码后,现在它已成功执行...

    【讨论】:

    【解决方案3】:

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

    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 中的循环

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 2010-12-17
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多