您的位置:首页 > 编程语言 > VB

Excel VBA 2003和 Excel VBA 2007不兼容的一个例子: 文件搜索

2010-10-25 23:34 746 查看


Re: Application.filesearch Replacement For Office 2007

Hi John

Filesearch has indeed been removed but the (very) old Dir remains, so you can use this instead, an example of which follows:

Code:
Sub testit()

myvar = FileList("C:/")

For i = LBound(myvar) To UBound(myvar)

    Debug.Print myvar(i)

Next

End Sub

Function FileList(fldr As String, Optional fltr As String = "*.*") As Variant

Dim sTemp As String, sHldr As String

If Right$(fldr, 1) <> "/" Then fldr = fldr & "/"

sTemp = Dir(fldr & fltr)

If sTemp = "" Then

    FileList = Split("No files found", "|") 'ensures an array is returned

    Exit Function

End If

Do

    sHldr = Dir

    If sHldr = "" Then Exit Do

    sTemp = sTemp & "|" & sHldr

Loop

FileList = Split(sTemp, "|")

End Function


This doesn't search subfolders but that functionality could no
doubt be introduced. In the current spec you have to pass in the
filepath to search.

Richard

EDIT: amended function so Do Loop doesn't error



Last edited by RichardSchollar; June 24th, 2007 at 07:59
.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐