View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default how can I copy file names into cells

And another possible way, just change the "A2" to the first cell you want to
receive the filename and hyperlink:

Sub ListAndLinkToFiles()
'User definable values
Const FirstCell = "A2" ' first cell to get file name/link
'end of user definable values

Dim anyFileName As String ' internal use variable
Dim myFolderPath As String ' internal use variable
Dim rowOffset As Long ' pointer to next cell to put entry into

Application.FileDialog(msoFileDialogFolderPicker). Show
On Error Resume Next
myFolderPath = _
Application.FileDialog(msoFileDialogFolderPicker). SelectedItems(1) _
& Application.PathSeparator
If Err < 0 Then
Err.Clear
Exit Sub ' user cancelled
End If
On Error GoTo 0
'because Excel 2007 removed the FileSearch object,
'we'll do this the old fashioned way
'initialize anyFileName
anyFileName = Dir(myFolderPath & "*.*", vbNormal)
Do While anyFileName < "" ' Start the loop.
ActiveSheet.Range(FirstCell).Offset(rowOffset, 0).Formula = _
"=HYPERLINK(" & Chr(34) & myFolderPath & anyFileName & Chr(34) _
& "," & Chr(34) & anyFileName & Chr(34) & ")"
anyFileName = Dir ' Get next entry.
rowOffset = rowOffset + 1
Loop
End Sub


"SAM SEBAIHI" wrote:


I would like to have a macro that if I execute it, it will copy all the
existing file names from a specified directory into individual cells in the
sheet. each individual file name will be hyperlinked, if I click on it, the
file should open (they could be Word or pdf files..etc..)

I really appreciate your help in advance

Thank you,