Public oldNR As Long
Sub HyperlinkDirectory()
''''''''''''''''''''''''''''''''''''''''''Need reference -Microsoft Visual Basic for Applications Extensibility 5.3''''''''''''''''''''''''''''''''''''
Dim fPath As String
Dim fType As String
Dim fname As String
Dim filePath As String
Dim NR As Long
Dim AddLinks As Boolean
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "ALL FILES LIST" Then
Sheets("ALL FILES LIST").Cells.Delete
F = True
Exit For
Else
F = False
End If
Next
If Not F Then Sheets.Add.Name = "ALL FILES LIST"
Sheets("ALL FILES LIST").Select
'''''''''''''''Select folder
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
If .SelectedItems.Count > 0 Then
fPath = .SelectedItems(1) & "\"
Else
Exit Sub
End If
End With
Range("B1").Value = fPath
'''''''''''''''''Types of files
fType = "*"
If fType = "False" Then Exit Sub
'
''''''''''''''''''Option to create hyperlinks
AddLinks = vbYes
'''''''''''''''''Create report
Application.ScreenUpdating = False
NR = 4
With ActiveSheet
.Range("A:C").Clear
.[A2] = "LIST OF FILES"
Call FindFilesAndAddLinks(fPath, fType, NR, AddLinks)
Range("B1").Value = fPath
End With
With ActiveSheet
.Range("A:B").Columns.AutoFit
.Range("B:B").HorizontalAlignment = xlCenter
With ActiveSheet
Range("A2").Select
Selection.Font.Bold = True
End With
Columns("A:A").Select
Selection.Font.Underline = xlUnderlineStyleNone
End With
Application.ScreenUpdating = True
End Sub
Private Sub FindFilesAndAddLinks(fPath As String, fType As String, ByRef NR As Long, AddLinks As Boolean)
Dim fname As String
Dim oFS As New FileSystemObject
Dim oDir
Dim fnamePath As String
'Files under current dir
On Error Resume Next
fname = Dir(fPath & "*." & fType)
With ActiveSheet
'Write folder name
.Range("A" & NR) = fPath
.Range("A" & NR).Select
If AddLinks Then .Hyperlinks.Add Anchor:=Selection, _
Address:=fPath, _
TextToDisplay:="FOLDER NAME: " & " " & UCase(Split(fPath, "\")(UBound(Split(fPath, "\")) - 1))
Selection.Font.Bold = True
Selection.Font.Size = 10
Selection.Font.Name = "Arial"
Selection.Font.Underline = xlUnderlineStyleNone
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
NR = NR + 1
Do While Len(fname) > 0
'filename
If .Range("A" & NR) <> "" Then Debug.Print "Overwriting " & NR
.Range("A" & NR) = fname
'modified
.Range("B" & NR) = fname
.Range("C" & NR) = fnamePath
'hyperlink
.Range("A" & NR).Select
If AddLinks Then .Hyperlinks.Add Anchor:=Selection, _
Address:=fPath & fname, _
TextToDisplay:=fname
'set for next entry
NR = NR + 1
fname = Dir
Loop
'Files under sub dir
Set oDir = oFS.GetFolder(fPath)
For Each oSub In oDir.subfolders
NR = NR + 1
Call FindFilesAndAddLinks(oSub.Path & "\", fType, NR, AddLinks)
Next oSub
End With
End Sub