本帖最后由 cheshirekitten 于 2022-6-9 12:43 编辑
功能介绍:
提取文件夹中所有文件、文件夹名称及所在地址
使用步骤,如图:
操作01:窗口界面
操作02:点开界面
操作03:提取地址生成文件
操作04:点开-文件名已提取
窗口界面代码如图:
论坛插入用的vb语言,代码分为两部分,如下:
[Visual Basic] 纯文本查看 复制代码 Sub FSO_FileExtraction()
Dim strFldPath As String '定义文件夹路径变量
With Application.FileDialog(msoFileDialogFolderPicker) '用户选择指定文件夹
.Title = "请选择指定文件夹。"
If .Show Then
strFldPath = .SelectedItems(1)
Else
Exit Sub '如果用户没有指定文件夹,则退出程序
End If
End With
Application.ScreenUpdating = False '关闭屏幕刷新
Range("a:b").ClearContents
Range("a1:b1") = Array("文件夹", "文件名及超链接")
Call ExtractionFileAddHyperlinks(strFldPath) '调取文件提取及增加超链接的函数
Range("a:b").EntireColumn.AutoFit '自动列宽
Sheets("Sheet1").Select
Sheets("Sheet1").Copy
ActiveSheet.Shapes.Range(Array("Button 1")).Delete
ActiveWorkbook.SaveAs Filename:= _
strFldPath & "\文档目录.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
Application.ScreenUpdating = True '打开屏幕刷新
Range("a:c").ClearContents
Columns("A:C").ColumnWidth = 8.08
ThisWorkbook.Save
End Sub
----------------------------------------
Sub 文件提取()
'子函数
'ExtractionFileAddHyperlinks 如下图所示:
Function ExtractionFileAddHyperlinks(ByVal strFldPath As String) As String
Dim objMyFSO As Object '定义变量
Dim objFld As Object
Dim objFile As Object
Dim objSubFld As Object
Dim strFilePath As String
Dim lngLastRow As Long
Dim intNum As Integer
Set objMyFSO = CreateObject("Scripting.FileSystemObject") '用直接创建法 创建FSO对象
Set objFld = objMyFSO.GetFolder(strFldPath) '调用FSO的GetFolder方法
For Each objFile In objFld.Files '遍历文件夹内的文件
lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
strFilePath = objFile.Path
intNum = InStrRev(strFilePath, "\") '使用instrrev函数获取最后文件夹名截至的位置
Cells(lngLastRow, 2) = Left(strFilePath, intNum - 1) '获取文件夹绝对地址
Cells(lngLastRow, 1) = Mid(strFilePath, intNum + 1) '获取文件名
' Cells(lngLastRow, 3) = "文件链接"
' ActiveSheet.Hyperlinks.Add Anchor:=Cells(lngLastRow, 3), Address:=strFilePath, ScreenTip:=strFilePath '增加超链接
Next objFile
For Each objSubFld In objFld.SubFolders '遍历文件夹内的子文件夹
Call ExtractionFileAddHyperlinks(objSubFld.Path) '递归调用
Next objSubFld
Set objMyFSO = Nothing '清空对象变量
Set objFld = Nothing
Set objFile = Nothing
Set objSubFld = Nothing
End Function
|