如何在Excel 2010中使用VBA在目录中获取最后修改的文件 [英] How to get the last modified file in a directory using VBA in Excel 2010

查看:198
本文介绍了如何在Excel 2010中使用VBA在目录中获取最后修改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以前在Excel 2003中使用Application.FileSearch方法可以实现这一点,但是这样做有可能在Excel 2010中使用VBA。被贬值(见下文)

  Dim sFileName As String 

sFileName =
With Application .FileSearch
.NewSearch
.LookIn = sDir
.Filename =*。*
.Execute msoSortByLastModified,msoSortOrderDescending

如果PublishFiles.Count > 0然后sFileName = EditorialFiles(1)

结束

任何想法如何在Excel 2010中执行此操作?



谢谢

解决方案

使用FileSystemObject是可以接受的,您可以使用 here



总结:

  Dim fso As Scripting。 FileSystemObject 
Dim fol As Scripting.Folder
Dim fdr As Scripting.Folder
Dim fil As Scripting.File
Dim flc As Scripting.Folders

Set fso = CreateObject(Scripting.FileSystemObject)
设置fol = fso.GetFolder(YourPathName)
设置flc = fol.SubFolders

对于每个fdr在flc
对于每个文件在fdr.Files
Debug.Print fil.DateLastModified
下一个fil
下一个fdr

设置fso = Nothing
设置fol =没有
设置flc =没有


Im looking for a way to do this in Excel 2010 using VBA.

It used to be possible in Excel 2003 using the Application.FileSearch method, but this has be depreciated. (see below)

Dim sFileName As String

sFileName = ""
With Application.FileSearch
    .NewSearch
    .LookIn = sDir
    .Filename = "*.*"
    .Execute msoSortByLastModified, msoSortOrderDescending

    If .FoundFiles.Count > 0 Then sFileName = .FoundFiles(1)

End With

Any ideas how to do this in Excel 2010?

Thanks

解决方案

If using the FileSystemObject is acceptable, you could use the method described here.

To summarize:

Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim fdr As Scripting.Folder
Dim fil As Scripting.File
Dim flc As Scripting.Folders

Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("YourPathName")
Set flc = fol.SubFolders

For Each fdr In flc
  For Each fil In fdr.Files
        Debug.Print fil.DateLastModified
  Next fil
Next fdr

Set fso = Nothing
Set fol = Nothing
Set flc = Nothing

这篇关于如何在Excel 2010中使用VBA在目录中获取最后修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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