|
我把取得文件夹下(包括子文件夹)所有PDF文件的清单,封装为一个自定义函数;
好用
- Public Function ListFiles(ByVal folderPath As String, ByRef counter As Integer)
- '子函数-取得文件夹下的PDF文件名,包括子文件夹,不带限制范围
- Dim FS As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Dim folder As Object
- Set folder = FS.GetFolder(folderPath)
- Dim file As Object
- ' Dim ws As Worksheet
- Dim rng As Range
- Dim arr() As Variant
- Dim i As Integer
- Dim lastRow As Long
- Dim result As String ' 假设result的类型为String
- ' Set ws = Sheet3 ' 修改为你的工作表名
- For Each file In folder.Files '
- ' 在这里执行相等的操作, '把取得数据的落到当前工作表上
- hzm = IIf(InStrRev(file.Name, ".") <> 0, Mid(file.Name, InStrRev(file.Name, ".") + 1, Len(file.Name)), "")
- If UCase(hzm) = "PDF" Then ' 过滤是PDF的
- ActiveSheet.Cells(counter, 1) = counter - 1
- ActiveSheet.Cells(counter, 2) = file.Name
- ActiveSheet.Cells(counter, 2).Hyperlinks.Add Anchor:=ActiveSheet.Cells(counter, 2), Address:=file.path
- ActiveSheet.Cells(counter, 3) = folder '文件夹
- counter = counter + 1
- End If
- Next file
- Dim subFolder As Object
- For Each subFolder In folder.SubFolders
- ListFiles subFolder.path, counter
- Next subFolder
- End Function
复制代码
|
|