如何在Mac上的给定文件夹中读取文件名?
我的目标是阅读给定文件夹中所有PNG文件的文件名。
我使用Windows VBA代码,该代码使用ActiveX FileSystemobject。
在Mac上,此代码导致
“运行时错误429 Activex组件无法创建对象”
Function ReadFileNames(ByVal sPath As String) As Integer
Dim oFSO, oFolder, oFile As Object
Dim sFileName As String
Set oFSO = CreateObject("scripting.FileSystemObject")
Set oFolder = oFSO.getfolder(sPath)
For Each oFile In oFolder.Files
If Not oFile Is Nothing And Right(LCase(oFile.Name), 4) = ".png" Then ' read only PNG-Files
sFileName = oFile.Name
' do something with the FileName ...
End If
Next oFile
End Function
My goal is to read the FileNames of all png files in a given folder.
I've Windows VBA code which uses the ActiveX FileSystemObject.
On a MAC This code results in
"runtime error 429 activex component can't create object"
Function ReadFileNames(ByVal sPath As String) As Integer
Dim oFSO, oFolder, oFile As Object
Dim sFileName As String
Set oFSO = CreateObject("scripting.FileSystemObject")
Set oFolder = oFSO.getfolder(sPath)
For Each oFile In oFolder.Files
If Not oFile Is Nothing And Right(LCase(oFile.Name), 4) = ".png" Then ' read only PNG-Files
sFileName = oFile.Name
' do something with the FileName ...
End If
Next oFile
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个使用本机VBA DIR命令的子,通过在调试窗口上打印其名称,将Excel Workbook列出:
这有帮助吗?在
更新中:doevents命令允许Excel处理其他未决的用户界面活动,例如窗口刷新,鼠标单击。如果您的文件夹中有很多文件(成千上万个),那么Excel可能会在这样的循环中显得无反应/冻结。这不是必需的,因为一旦完成循环,它将再次响应。如果您只有几百个文件,那么它是一个过度杀伤。删除并尝试。
Here is a sub, using the native VBA DIR command, listing EXCEL workbooks in a folder by printing their names on the debug window:
Does this help? In
Update: doevents command allows Excel to process other pending user interface activities, such as window refreshes, mouse-clicks. If you have lots of files (thousands) in a folder, Excel may appear unresponsive/frozen in a loop like this. It is not necessary, as it will become responsive again, once it completes the loop. If you have only a few hundred files then it is an overkill. Remove and try.
Mac的VBA可以链接到整个C标准库,例如此示例:
我懒得为您写相关示例,但是如果您偶然地使用C标准库进行文件操作,则可以那样做。
VBA for Mac can link to the entire c standard library, like this example:
I'm too lazy to write out relevant examples for you, but if, by chance, you are familiar with using the c standard library for file manipulation, you can just do it that way.