I got the following codes from others.
Would appreciate if someone could improve the following codes:
1. To have a column (to the left of column 'Name'), which shows the folder path and with hyperlink. i.e. if the file is found at G drive folder called 'Sound', it will show G:\Sound. when i click on the hyperlink, it will open this folder.
2. To insert hyperlink for each individual files under column 'Name', so that when hyperlink is clicked, it will open the file.
Thanks!
-----------------------------------------------------------------------
Option Explicit
'API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Sub FileInfo()
Dim c As Long, r As Long, i As Long
Dim FileName As Object 'FolderItem2
Dim objShell As Object 'IShellDispatch4
Dim objFolder As Object 'Folder3
' Create the object
Set objShell = CreateObject("Shell.Application")
' Prompt for the folder
Set objFolder = objShell.Namespace(GetDirectory)
' Insert headers on active sheet
Worksheets.Add
c = 0
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(1, c) = objFolder.GetDetailsOf(objFolder.Items, i)
End If
Next i
' Loop through the files
r = 1
For Each FileName In objFolder.Items
c = 0
r = r + 1
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(r, c) = objFolder.GetDetailsOf(FileName, i)
End If
Next i
Next FileName
' Make it a table
ActiveSheet.ListObjects.Add xlSrcRange, _
Range("A1").CurrentRegion
End Sub
Function GetDirectory(Optional Msg) As String
' Returns the directory specified by the user
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Would appreciate if someone could improve the following codes:
1. To have a column (to the left of column 'Name'), which shows the folder path and with hyperlink. i.e. if the file is found at G drive folder called 'Sound', it will show G:\Sound. when i click on the hyperlink, it will open this folder.
2. To insert hyperlink for each individual files under column 'Name', so that when hyperlink is clicked, it will open the file.
Thanks!
-----------------------------------------------------------------------
Option Explicit
'API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Sub FileInfo()
Dim c As Long, r As Long, i As Long
Dim FileName As Object 'FolderItem2
Dim objShell As Object 'IShellDispatch4
Dim objFolder As Object 'Folder3
' Create the object
Set objShell = CreateObject("Shell.Application")
' Prompt for the folder
Set objFolder = objShell.Namespace(GetDirectory)
' Insert headers on active sheet
Worksheets.Add
c = 0
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(1, c) = objFolder.GetDetailsOf(objFolder.Items, i)
End If
Next i
' Loop through the files
r = 1
For Each FileName In objFolder.Items
c = 0
r = r + 1
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(r, c) = objFolder.GetDetailsOf(FileName, i)
End If
Next i
Next FileName
' Make it a table
ActiveSheet.ListObjects.Add xlSrcRange, _
Range("A1").CurrentRegion
End Sub
Function GetDirectory(Optional Msg) As String
' Returns the directory specified by the user
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Last edited: