VBA中遍历文件夹

http://cuidamian.blog.163.com/blog/static/9532228201002921518730/

 

'首先,打开Office的宏编辑器,选择“工具”-“引用”,选中下面两个对象:

'Microsoft Scripting Runtime
'Microsoft Script Control 1.0

'代码如下:

Sub Test()
    EnumFiles "C:/Documents and Settings/ibmsz/My Documents/2010-08-10"    ' 遍历 e: 下的所有文件
End Sub

Sub EnumFiles(ByVal sPath As String)
    Dim fs As New FileSystemObject
    GetFile fs.GetFolder(sPath)
    Set fs = Nothing
End Sub

Sub GetFile(ByVal fldParent As Folder)
    Dim fldSub As Folder, fSub As File
    For Each fldSub In fldParent.SubFolders
        GetFile fldSub
    Next
    For Each fSub In fldParent.Files
        If UCase(Right(Trim(fSub), 3)) = "DOC" Then     ' 文件类型判断
            Debug.Print fSub
        End If
    Next
    Set fldSub = Nothing
    Set fSub = Nothing
End Sub

 

你可能感兴趣的:(String,Microsoft,Office,each,VBA,scripting)