无法使用FSO打开文件 [英] Unable to open file using FSO

查看:123
本文介绍了无法使用FSO打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FileSystemObject打开文件,但是当我尝试运行它时,系统什么都不做。不显示调试,没有运行时错误,不编译...它只是保持不变。我还参考了参考资料中的MS Scripting Runtime。



我尝试过:



  Sub  fsoobject()
Dim fso 作为 FileSystemObject,f As 文件夹,sf 作为文件夹,myFile 作为文件
设置 f = fso.GetFolder( C:\ Users \ jpmehta \Desktop

对于 每个 sf f.SubFolders
对于 每个 mySubFolder myFolder.SubFolders
对于 每个 myFile mySubFolder.Files
如果 myFile.Name 完成 然后
MsgBox myFile.Name
退出 对于
结束 如果
下一步

MsgBox Else
下一步
下一步

结束 Sub

解决方案

< blockquote>你可能要写

 对于 每个 mySubFolder   sf 。 SubFolders 





(编辑:这就是为什么我不喜欢不强制执行变量声明的语言)


除了phil.o的解决方案之外,这里还有一个工作解决方案,它将遍历起始文件夹的所有子文件夹并列出所有文件。它并不能完全满足您的需求,但它确实演示了如何递归调用函数。

 选项 明确 
Sub fsoobject()
Dim fso 作为 FileSystemObject,f 作为文件夹
设置 f = fso.GetFolder( C:\Temp
Debug.Print 开始...
重复f
Debug.Print ...结束
结束 Sub
Sub recurs(文件夹 As 文件夹)
Dim subFolder 作为文件夹
对于 每个 subFolder folder.SubFolders
recurs subFolder
下一步
Dim myFile 作为文件
对于 每个 myFile 文件夹中。文件
Debug.Print folder.Name& & myFile.Name
下一步
结束 Sub

注意使用 Option Explicit - 我强烈建议你总是包含该行你的VBA代码。您可以将其设置为VBE中的默认设置。



根据我的评论,如果您的代码无法编译,那么您必须解决这些问题第一的。这就是为什么我不喜欢被解释的语言(比如VBA),因为你实际上可以执行错误的代码并且只在崩溃时才知道问题。



最后,学会使用断点并使用调试器逐步执行代码 - 问题很清楚。


  Sub  fsoobject()
Dim fso As FileSystemObject,f 作为文件夹,sf 作为文件夹,myFile < span class =code-keyword> As 文件
设置 f = fso.GetFolder( C:\ Users \ jpmehta \Desktop

' For each sf in f.SubFolders
' For each mySubFolder in sf.SubFolders
' For my myFile in mySubFolder.Files
' if myFile .Name喜欢完成然后
' MsgBox myFile.Name
' 退出
' 结束如果
' 下一步
'
' MsgBoxElse
' 下一步
' 下一步

Dim 文件
对于 每个文件 f.Files
如果 File.Name = Done.PNG 然后
调用 Shell( Explorer.exe& File.Path& ,vbNormalFocus)
退出 对于
结束 如果
下一步

结束 Sub







Sub fsoobject()

Dim fso As New FileSystemObject,f As Folder,sf As Folder,myFile As File

设置f = fso.GetFolder(C:\ Users \ jpmehta \Desktop)



'对于每个sf在f。子文件夹

'为每个mySubFolder在sf.SubFolders中

'为每个myFile在mySubFolder.Files

'如果myFile.Name喜欢完成然后

'MsgBox myFile.Name

'Exi t

'结束如果

'Next

'

'MsgBoxElse

'下一页

'下一页



昏暗文件

每个文件在f.Files

如果File.Name =Done.PNG那么

调用Shell(Explorer.exe& File.Path& ,vbNormalFocus)

退出

结束如果

下一页



End Sub

嗯,我搜索了一下,发现这个关键字Shell打开一个文件。在应用此代码时,现在它已成功执行...


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:

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

解决方案

You may have to write

For Each mySubFolder In sf.SubFolders


instead.
(Edit: that's why I don't like languages which do not enforce the declaration of variables)


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.

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.


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


这篇关于无法使用FSO打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆