|
自己也写过类似的在2007版下遍历文件夹提取文件名的代码,用的是递归的方法,写法就相对简单一点,不过可能资源消耗会比较大。- Option Explicit
- Sub Test()
- Dim iPath As String, i As Long
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "请选择要查找的文件夹"
- If .Show Then
- iPath = .SelectedItems(1)
- End If
- End With
-
- If iPath = "False" Or Len(iPath) = 0 Then Exit Sub
-
- i = 1
- Call GetFolderFile(iPath, i)
- MsgBox "文件名链接获取完毕。", vbOKOnly, "提示"
- End Sub
- Private Sub GetFolderFile(ByVal nPath As String, ByRef iCount As Long)
- Dim iFileSys As New FileSystemObject
- Dim iFile As Files, gFile As File
- Dim iFolder As Folder, sFolder As Folders, nFolder As Folder
- Set iFolder = iFileSys.GetFolder(nPath)
- Set sFolder = iFolder.SubFolders
- Set iFile = iFolder.Files
- With ActiveSheet
- For Each gFile In iFile
- .Hyperlinks.Add anchor:=.Cells(iCount, 1), Address:=gFile.Path, TextToDisplay:=gFile.Name
- iCount = iCount + 1
- Next
- End With
-
- '递归遍历所有子文件夹
- For Each nFolder In sFolder
- Call GetFolderFile(nFolder.Path, iCount)
- Next
- End Sub
复制代码
[ 本帖最后由 chentx 于 2010-11-22 22:28 编辑 ] |
|