Hi Kev,
I think the code is working now but the files are not opening. As you can see in the excel list only those files that matched has the hyperlink.
I did insert this Code
MsgBox = Replace(c.Text, " ", "") & ".pdf"
After this line but I'm getting an error.
For Each c In Selection
MsgBox = Replace(c.Text, " ", "") & ".pdf"
Set myFolder = FSO.GetFolder(sPath)
For Each myfile In myFolder.Files
If myfile.Name = c.Value & ".pdf" Then
sAddress = myFolder.Path & "\" & Replace(c.Text, " ", "") & ".pdf"
c.Parent.Hyperlinks.Add Anchor:=c, Address:=sAddress, TextToDisplay:=c.Text
GoTo TryNextCell
This is the Code I'm using now, sorry I really don't have any knowledge about VBA coding:
Function FindFiles(sPath As String) As String
'require reference Microsoft Scripting Runtime
Dim FSO As New FileSystemObject
Dim myFolder As Folder
Dim mySubFolder As Folder
Set Rng = Selection
For Each c In Selection
Set myFolder = FSO.GetFolder(sPath)
For Each myfile In myFolder.Files
If myfile.Name = c.Value & ".pdf" Then
sAddress = myFolder.Path & "\" & Replace(c.Text, " ", "") & ".pdf"
c.Parent.Hyperlinks.Add Anchor:=c, Address:=sAddress, TextToDisplay:=c.Text
GoTo TryNextCell
End If
Next myfile
For Each mySubFolder In myFolder.SubFolders
For Each myfile In mySubFolder.Files
If myfile.Name = c.Value & ".pdf" Then
sAddress = mySubFolder.Path & "\" & Replace(c.Text, " ", "") & ".pdf"
c.Parent.Hyperlinks.Add Anchor:=c, Address:=sAddress, TextToDisplay:=c.Text
GoTo TryNextCell
End If
Next myfile
FindFiles = FindFiles(mySubFolder.Path)
Next
TryNextCell:
Next c
End Function
Sub AddLinks()
Dim folderToSearch As String, a As Integer, myArray
myArray = Array("E:\HBK\PROJECTS\TDO-16-TTC-0018\DESIGN DATA\01_SCHEMATIC DESIGN\IRSD#1 - 17-Jan-17\")
For a = 0 To UBound(myArray)
folderToSearch = myArray(a)
Call FindFiles(folderToSearch)
Next a
End Sub
Bookmarks