-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpdf
40 lines (33 loc) · 1.23 KB
/
getpdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Sub ListPDFFiles()
Dim fso As New FileSystemObject
Dim startFolder As String
Dim targetSheet As Worksheet
Dim row As Integer
' Set the starting folder
startFolder = "C:\YourFolderPath" ' Change to your folder path
' Setup the worksheet
Set targetSheet = ThisWorkbook.Sheets("Sheet1") ' Change to your target sheet
targetSheet.Cells.Clear
row = 1
' Call the recursive procedure
ListFiles startFolder, fso, targetSheet, row
MsgBox "Finished listing PDF files."
End Sub
Sub ListFiles(folderPath As String, fso As FileSystemObject, ByRef targetSheet As Worksheet, ByRef row As Integer)
Dim folder As Folder, subFolder As Folder
Dim file As file
' Get the folder object
Set folder = fso.GetFolder(folderPath)
' Check each file in the folder
For Each file In folder.Files
If LCase(fso.GetExtensionName(file.Name)) = "pdf" Then
' If file is a PDF, write its path to Excel
targetSheet.Cells(row, 1).Value = file.Name
row = row + 1
End If
Next file
' Recursively search subfolders
For Each subFolder In folder.SubFolders
ListFiles subFolder.Path, fso, targetSheet, row
Next subFolder
End Sub