首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用fso打开文件

无法使用fso打开文件
EN

Stack Overflow用户
提问于 2017-10-30 12:02:20
回答 3查看 1.9K关注 0票数 0

我试图使用FileSystemObject打开一个文件,但是当我试图运行它时,系统什么也不做。没有显示Debug,没有运行时错误,也没有编译。它只会保持原样。我还检查了参考资料中的"MS脚本运行时“。下面是我为此使用的代码:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-30 13:03:25

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2017-10-30 13:02:20

代码语言:javascript
复制
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“来打开一个文件。在应用这段代码之后,现在它成功地执行了.

票数 0
EN

Stack Overflow用户

发布于 2017-10-30 13:10:34

这个密码适用于我。清晰而简单:

代码语言:javascript
复制
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. set子文件夹(Set ssf = sf)
  2. 我建议使用Debug.Print检查VBE中的循环。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47014782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档