INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Looking for a file in a folder and creating associated hyperlink

Looking for a file in a folder and creating associated hyperlink

(OP)
Hi all,

I use an excel file as BoM and I would like to look through our network drive to find associated drawing and create hyperlink to it in another column. All and every revision of the drawings are stored in the same place (ERP database) and I indeed need the link to the latest one.

I know how to get the file name I am looking for and also look through the directory but it seems my macro never find any result. I assume the mistake comes from my comparison formula "If InStr(FileName, FileSearch) > 0 Then" but I'm not sure.

Here is the code :

CODE -->

Sub LoopThroughDrawingFiles()
    Dim FilePath, FileName, FileSearch As String
    FilePath = "V:\ERP-doc\Doc archive"
    FileName = Dir(FilePath & "\*.pdf")
    FileName = CStr(FileName)
    For ligne = 1 To 400
        FileSearch = Left(Range("B" & ligne).Value, 14)
        Do While (FileName <> "")
            If InStr(FileName, FileSearch) > 0 Then
                ActiveSheet.Hyperlinks.Add Anchor:=Range("F" & ligne), Address:=FilePath & FileName, TextToDisplay:=FileSearch
                Exit Sub
            End If
            FileName = Dir
        Loop
    Next ligne
End Sub 

Any help very welcome :)

Cyril Guichard
Defense Program Manager
Belgium

RE: Looking for a file in a folder and creating associated hyperlink

(OP)
Ok guys, finally sorted it out by myself. Works like a charm

CODE -->

Sub LoopThroughDrawingFiles()
    Dim FilePath, FileName, FileSearch As String
    For ligne = 4 To 400
        FilePath = "V:\ERP-doc\Doc archive"
        FileName = Dir(FilePath & "\*.pdf")
        FileName = CStr(FileName)
        FileSearch = Left(Range("B" & ligne).Value, 15)
        If Len(FileSearch) > 14 Then
            Do While (FileName <> "")
                If InStr(FileName, FileSearch) > 0 Then
                    ActiveSheet.Hyperlinks.Add Anchor:=Range("F" & ligne), Address:=FilePath & "\" & FileName, TextToDisplay:=Left(FileName, Len(FileName) - 6)
                    FileName = Left(FileName, Len(FileName) - 4)
                    Range("G" & ligne).Value = Right(FileName, 1)
                End If
                FileName = Dir
            Loop
        End If
    Next ligne
End Sub 

Cyril Guichard
Space Program Manager
Belgium

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close