Guest

PDF sort

Nov 26th, 2026
47
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 28.96 KB | None | 0 0
  1. Option Explicit
  2.  
  3. '================================================
  4. ' MAIN MACRO - SORT PDF FOLDERS & GENERATE REPORT
  5. '================================================
  6.  
  7. Sub SortAndGeneratePDFReport()
  8.  
  9. Dim ws As Worksheet
  10. Dim fso As Object
  11. Dim rootFolder As Object
  12. Dim subFolder As Object
  13. Dim fileItem As Object
  14. Dim folderList As Collection
  15. Dim dist As Object
  16.  
  17. Dim mainPath As String
  18. Dim sourcePath As String
  19. Dim destPath As String
  20. Dim bucketName As String
  21. Dim providerName As String
  22. Dim folderName As String
  23. Dim folderKey As Variant
  24.  
  25. Dim pdfCount As Long
  26. Dim totalPDFs As Long
  27. Dim totalFolders As Long
  28. Dim movedFolders As Long
  29. Dim skippedFolders As Long
  30.  
  31. Dim lastRow As Long
  32. Dim detailRow As Long
  33. Dim summaryRow As Long
  34. Dim distRow As Long
  35.  
  36. Dim i As Long
  37. Dim statusText As String
  38. Dim errorText As String
  39.  
  40. Dim oldScreen As Boolean
  41. Dim oldAlerts As Boolean
  42.  
  43. Set ws = ActiveSheet
  44. Set fso = CreateObject("Scripting.FileSystemObject")
  45.  
  46. oldScreen = Application.ScreenUpdating
  47. oldAlerts = Application.DisplayAlerts
  48.  
  49. On Error GoTo FatalError
  50.  
  51. Application.ScreenUpdating = False
  52. Application.DisplayAlerts = False
  53.  
  54. 'Clear old reports only
  55. ws.Range("C:S").ClearContents
  56.  
  57. 'Report headings
  58. ws.Range("C1:H1").Value = Array( _
  59. "Provider Path", _
  60. "Provider Name", _
  61. "Subfolder Name", _
  62. "PDF Count", _
  63. "Destination Path", _
  64. "Status")
  65.  
  66. ws.Range("J1:O1").Value = Array( _
  67. "Provider", _
  68. "Total Subfolders", _
  69. "Total PDFs", _
  70. "Moved", _
  71. "Skipped", _
  72. "Invalid Paths")
  73.  
  74. ws.Range("Q1:S1").Value = Array( _
  75. "Provider", _
  76. "PDFs Per Folder", _
  77. "Number Of Folders")
  78.  
  79. detailRow = 2
  80. summaryRow = 2
  81. distRow = 2
  82.  
  83. lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  84.  
  85. For i = 1 To lastRow
  86.  
  87. mainPath = Trim(CStr(ws.Cells(i, "A").Value))
  88.  
  89. If mainPath <> "" Then
  90.  
  91. If fso.FolderExists(mainPath) Then
  92.  
  93. Set rootFolder = fso.GetFolder(mainPath)
  94. providerName = rootFolder.Name
  95.  
  96. Set folderList = New Collection
  97. Set dist = CreateObject("Scripting.Dictionary")
  98.  
  99. totalFolders = 0
  100. totalPDFs = 0
  101. movedFolders = 0
  102. skippedFolders = 0
  103.  
  104. 'Collect original subfolders
  105. For Each subFolder In rootFolder.SubFolders
  106.  
  107. If Not IsPDFBucket(subFolder.Name) Then
  108. folderList.Add subFolder.Path
  109. End If
  110.  
  111. Next subFolder
  112.  
  113. 'Process folders
  114. For Each folderKey In folderList
  115.  
  116. sourcePath = CStr(folderKey)
  117. folderName = fso.GetFolder(sourcePath).Name
  118.  
  119. pdfCount = 0
  120. statusText = ""
  121. destPath = ""
  122.  
  123. totalFolders = totalFolders + 1
  124.  
  125. 'Count direct PDF files
  126. For Each fileItem In fso.GetFolder(sourcePath).Files
  127.  
  128. If LCase(fso.GetExtensionName(fileItem.Name)) = "pdf" Then
  129. pdfCount = pdfCount + 1
  130. End If
  131.  
  132. Next fileItem
  133.  
  134. totalPDFs = totalPDFs + pdfCount
  135.  
  136. 'Distribution count
  137. If dist.Exists(CStr(pdfCount)) Then
  138. dist(CStr(pdfCount)) = dist(CStr(pdfCount)) + 1
  139. Else
  140. dist.Add CStr(pdfCount), 1
  141. End If
  142.  
  143. If pdfCount = 0 Then
  144.  
  145. statusText = "Skipped - No PDF"
  146. skippedFolders = skippedFolders + 1
  147.  
  148. Else
  149.  
  150. If pdfCount = 1 Then
  151. bucketName = "1 PDF"
  152. Else
  153. bucketName = pdfCount & " PDFs"
  154. End If
  155.  
  156. destPath = fso.BuildPath(mainPath, bucketName)
  157.  
  158. 'Create destination folder
  159. If Not fso.FolderExists(destPath) Then
  160. fso.CreateFolder destPath
  161. End If
  162.  
  163. 'Prevent overwriting existing folders
  164. If fso.FolderExists( _
  165. fso.BuildPath(destPath, folderName)) Then
  166.  
  167. statusText = "Skipped - Name Conflict"
  168. skippedFolders = skippedFolders + 1
  169.  
  170. Else
  171.  
  172. On Error Resume Next
  173.  
  174. Err.Clear
  175.  
  176. fso.MoveFolder sourcePath, _
  177. fso.BuildPath(destPath, folderName)
  178.  
  179. If Err.Number = 0 Then
  180.  
  181. statusText = "Moved Successfully"
  182. movedFolders = movedFolders + 1
  183.  
  184. Else
  185.  
  186. statusText = "Error: " & Err.Description
  187. skippedFolders = skippedFolders + 1
  188.  
  189. End If
  190.  
  191. Err.Clear
  192. On Error GoTo FatalError
  193.  
  194. End If
  195.  
  196. End If
  197.  
  198. 'Detailed report
  199. ws.Cells(detailRow, "C").Value = mainPath
  200. ws.Cells(detailRow, "D").Value = providerName
  201. ws.Cells(detailRow, "E").Value = folderName
  202. ws.Cells(detailRow, "F").Value = pdfCount
  203. ws.Cells(detailRow, "G").Value = destPath
  204. ws.Cells(detailRow, "H").Value = statusText
  205.  
  206. detailRow = detailRow + 1
  207.  
  208. Next folderKey
  209.  
  210. 'Provider summary
  211. ws.Cells(summaryRow, "J").Value = providerName
  212. ws.Cells(summaryRow, "K").Value = totalFolders
  213. ws.Cells(summaryRow, "L").Value = totalPDFs
  214. ws.Cells(summaryRow, "M").Value = movedFolders
  215. ws.Cells(summaryRow, "N").Value = skippedFolders
  216. ws.Cells(summaryRow, "O").Value = 0
  217.  
  218. summaryRow = summaryRow + 1
  219.  
  220. 'PDF quantity distribution
  221. For Each folderKey In dist.Keys
  222.  
  223. ws.Cells(distRow, "Q").Value = providerName
  224. ws.Cells(distRow, "R").Value = _
  225. folderKey & " PDF(s)"
  226. ws.Cells(distRow, "S").Value = dist(folderKey)
  227.  
  228. distRow = distRow + 1
  229.  
  230. Next folderKey
  231.  
  232. Else
  233.  
  234. 'Invalid path report
  235. ws.Cells(detailRow, "C").Value = mainPath
  236. ws.Cells(detailRow, "H").Value = "Invalid Path"
  237.  
  238. ws.Cells(summaryRow, "J").Value = mainPath
  239. ws.Cells(summaryRow, "O").Value = 1
  240.  
  241. detailRow = detailRow + 1
  242. summaryRow = summaryRow + 1
  243.  
  244. End If
  245.  
  246. End If
  247.  
  248. Next i
  249.  
  250. 'Format all reports
  251. FormatPDFReports ws
  252.  
  253. Application.ScreenUpdating = oldScreen
  254. Application.DisplayAlerts = oldAlerts
  255.  
  256. MsgBox "PDF SORTING COMPLETED!" & vbCrLf & vbCrLf & _
  257. "Provider Reports Generated Successfully." & vbCrLf & _
  258. "Check columns C:S for complete reports.", _
  259. vbInformation, "PDF Folder Manager"
  260.  
  261. Exit Sub
  262.  
  263. FatalError:
  264.  
  265. Application.ScreenUpdating = oldScreen
  266. Application.DisplayAlerts = oldAlerts
  267.  
  268. MsgBox "Process stopped: " & Err.Description, _
  269. vbExclamation, "PDF Manager"
  270.  
  271. End Sub
  272.  
  273.  
  274. '================================================
  275. ' IDENTIFY DESTINATION FOLDERS
  276. '================================================
  277.  
  278. Private Function IsPDFBucket(ByVal folderName As String) As Boolean
  279.  
  280. Dim txt As String
  281.  
  282. If Right$(folderName, 5) = " PDFs" Then
  283.  
  284. txt = Left$(folderName, Len(folderName) - 5)
  285.  
  286. ElseIf Right$(folderName, 4) = " PDF" Then
  287.  
  288. txt = Left$(folderName, Len(folderName) - 4)
  289.  
  290. Else
  291.  
  292. Exit Function
  293.  
  294. End If
  295.  
  296. If Len(txt) > 0 Then
  297. IsPDFBucket = IsNumeric(txt)
  298. End If
  299.  
  300. End Function
  301.  
  302.  
  303. '================================================
  304. ' REPORT FORMATTING
  305. '================================================
  306.  
  307. Private Sub FormatPDFReports(ByVal ws As Worksheet)
  308.  
  309. Dim headers As Variant
  310. Dim col As Variant
  311.  
  312. headers = Array("C1:H1", "J1:O1", "Q1:S1")
  313.  
  314. For Each col In headers
  315.  
  316. With ws.Range(CStr(col))
  317.  
  318. .Interior.Color = RGB(34, 139, 70)
  319. .Font.Color = vbWhite
  320. .Font.Bold = True
  321. .HorizontalAlignment = xlCenter
  322.  
  323. End With
  324.  
  325. Next col
  326.  
  327. ws.Columns("C:S").AutoFit
  328.  
  329. ws.Columns("C").ColumnWidth = 35
  330. ws.Columns("E").ColumnWidth = 25
  331. ws.Columns("G").ColumnWidth = 40
  332. ws.Columns("H").ColumnWidth = 25
  333.  
  334. ws.Columns("J:S").ColumnWidth = 20
  335.  
  336. End Sub
  337.  
  338.  
  339. '================================================
  340. ' CREATE GREEN FIXED BUTTON
  341. '================================================
  342.  
  343. Sub CreatePDFManagerButton()
  344.  
  345. Dim ws As Worksheet
  346. Dim btn As Shape
  347.  
  348. Set ws = ActiveSheet
  349.  
  350. On Error Resume Next
  351. Set btn = ws.Shapes("PDFManagerButton")
  352. On Error GoTo 0
  353.  
  354. If btn Is Nothing Then
  355.  
  356. Set btn = ws.Shapes.AddShape( _
  357. 5, _
  358. ws.Range("U2").Left, _
  359. ws.Range("U2").Top, _
  360. 220, _
  361. 55)
  362.  
  363. btn.Name = "PDFManagerButton"
  364.  
  365. End If
  366.  
  367. With btn
  368.  
  369. .Placement = xlFreeFloating
  370.  
  371. .OnAction = "'" & ThisWorkbook.Name & _
  372. "'!SortAndGeneratePDFReport"
  373.  
  374. .Fill.ForeColor.RGB = RGB(34, 139, 70)
  375. .Line.Visible = False
  376.  
  377. .TextFrame.Characters.Text = _
  378. "SORT & GENERATE REPORT"
  379.  
  380. .TextFrame.HorizontalAlignment = xlHAlignCenter
  381. .TextFrame.VerticalAlignment = xlVAlignCenter
  382.  
  383. With .TextFrame.Characters.Font
  384. .Bold = True
  385. .Size = 11
  386. .Color = vbWhite
  387. End With
  388.  
  389. End With
  390.  
  391. MsgBox "PDF Manager Button Created!", _
  392. vbInformation, "PDF Manager"
  393.  
  394. End Sub
RAW Paste Data Copied