dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
Our office manager wants to reduce the complexity of our office hard-drive as she feels there are too many sub-folders going to deep. Problem is, we have a large number of excel spreadsheets with VBA pointing to file locations. Therefore, I wish use VBA to be able to locate all VBA containing workbooks so that if necessary, I can amend any FilePath or FolderPath references in VBA controlled workbooks i.e., after the office folder structure has been 're-engineered'...
Basic problem:
I have found a macro that: a) searches folders and sub-folders and b) when if finds a wb with VBA, it adds the following to a worksheet: col 1 (Path); Col 2 (File Name); Col 3 (Hyperlink to file).
During testing, I identified the following issue: when a workbook is password protected without offering a read-only option, the code stops.
Please would you help me modify the RecursiveFolder macro below so that if it finds a password protected file, it skips checking for VBA and instead simply adds the file's Path, Filename and Hyperlink to the worksheet?
Kind regards,
Doug.
Full Code:
Our office manager wants to reduce the complexity of our office hard-drive as she feels there are too many sub-folders going to deep. Problem is, we have a large number of excel spreadsheets with VBA pointing to file locations. Therefore, I wish use VBA to be able to locate all VBA containing workbooks so that if necessary, I can amend any FilePath or FolderPath references in VBA controlled workbooks i.e., after the office folder structure has been 're-engineered'...
Basic problem:
I have found a macro that: a) searches folders and sub-folders and b) when if finds a wb with VBA, it adds the following to a worksheet: col 1 (Path); Col 2 (File Name); Col 3 (Hyperlink to file).
During testing, I identified the following issue: when a workbook is password protected without offering a read-only option, the code stops.
Please would you help me modify the RecursiveFolder macro below so that if it finds a password protected file, it skips checking for VBA and instead simply adds the file's Path, Filename and Hyperlink to the worksheet?
Kind regards,
Doug.
Full Code:
VBA Code:
Option Explicit
Dim wksDest As Worksheet
Dim NextRow As Long
Sub ListFiles()
Dim objFSO As Object
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
Application.EnableEvents = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wksDest = Worksheets("FilesWithMacros")
wksDest.Range("A1").Value = "Path"
wksDest.Range("B1").Value = "File Name"
wksDest.Range("C1").Value = "Hyperlink"
NextRow = wksDest.Cells(wksDest.Rows.Count, "A").End(xlUp).Row + 1
'Change the path to your folder, accordingly
Call RecursiveFolder(objFSO, "S:\APS_Logistics", True)
wksDest.Columns.AutoFit
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
End Sub
Sub RecursiveFolder(ByRef FSO As Object, ByVal MyPath As String, ByVal IncludeSubFolders As Boolean)
Dim File As Object
Dim Folder As Object
Dim SubFolder As Object
Dim Wkb As Workbook
Dim VBC As Object
Dim NumLines As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
Application.EnableEvents = False
Set Folder = FSO.GetFolder(MyPath)
For Each File In Folder.Files
If Right(File.Name, 4) = ".xls" Or Right(File.Name, 4) = ".xlt" Or Right(File.Name, 5) = ".xlsm" Or Right(File.Name, 5) = ".xltm" Or Right(File.Name, 5) = ".xlsb" Or Right(File.Name, 5) = ".xlam" Then
Set Wkb = Workbooks.Open(File.Path, Password:="", UpdateLinks:=xlUpdateLinksNever, Notify:=True)
On Error Resume Next
For Each VBC In Wkb.VBProject.VBComponents
With VBC.CodeModule
NumLines = .CountOfLines - .CountOfDeclarationLines
If NumLines > 0 Then
wksDest.Cells(NextRow, "A").Value = Wkb.Path
wksDest.Cells(NextRow, "B").Value = File.Name
wksDest.Hyperlinks.Add anchor:=wksDest.Cells(NextRow, "C"), Address:=File.Path, TextToDisplay:=File.Name
NextRow = NextRow + 1
Exit For
End If
End With
Next VBC
Wkb.Close savechanges:=False
End If
Next File
If IncludeSubFolders Then
For Each SubFolder In Folder.SubFolders
Call RecursiveFolder(FSO, SubFolder.Path, True)
Next SubFolder
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
End Sub