无法使用FSO打开文件

编程入门 行业动态 更新时间:2024-10-13 00:34:55
本文介绍了无法使用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

解决方案

你可能要写

对于 每个 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 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打开文件

本文发布于:2023-07-27 00:01:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   FSO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!