Hyperlink file in pdf format

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am using the code shown below to hyperlink some pdf files BUT now need an alteration.
The below code works fine if its just a customers name.

Now i have some files (250+) which are saved in the same folder like so,

TOM JONES 01-06-23 366A76
FRANK SINATRA 05-09-22 2DBD1C

So if i use the code shown below then i will see the Msg There is no file for this customer when looking for the above.
Reason being the code below is just looking for the customers name.

All the files consist of the customers name, the date & a 6 string code followed by .pdf

Is there a workaround so the code just looks for say TOM JONES when the file is actually saved as TOM JONES 01-06-23 366A75



VBA Code:
Private Sub HyperlinkDisco_Click()
 Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\"
        If ActiveCell.Column = Columns("B").Column Then
          
        If Len(Dir(FILE_PATH & ActiveCell.Value & ".pdf")) Then
        ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & ActiveCell.Value & ".pdf"
        MsgBox "HYPERLINK WAS SUCCESSFUL.", vbInformation, "POSTAGE SHEET HYPERLINK MESSAGE"
        End If
        
        Else
        MsgBox "PLEASE SELECT A CUSTOMER FIRST TO HYPERLINK THE FILE.", vbCritical, "POSTAGE SHEET DISCO II HYPERLINK MESSAGE"
        Exit Sub
        End If
        
        If Dir(FILE_PATH & ActiveCell.Value & ".pdf") = "" Then
        If MsgBox("THERE IS NO FILE FOR THIS CUSTOMER" & vbNewLine & "WOULD YOU LIKE TO OPEN THE DISCO II FOLDER ?", vbYesNo + vbCritical, "HYPERLINK CUSTOMER DISCO II MESSAGE.") = vbYes Then
        CreateObject("Shell.Application").Open ("C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\")
        End If

    
    End If
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is there a workaround so the code just looks for say TOM JONES when the file is actually saved as TOM JONES 01-06-23 366A75

Use the '*' wildcard character in the Dir string so it looks for "TOM JONES*.pdf".

Replace:

VBA Code:
        If Len(Dir(FILE_PATH & ActiveCell.Value & ".pdf")) Then
        ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & ActiveCell.Value & ".pdf"
with:

VBA Code:
Dim file As String
file = Dir(FILE_PATH & ActiveCell.Value & "*.pdf")
If Len(file) Then
    ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & file

and:

VBA Code:
        If Dir(FILE_PATH & ActiveCell.Value & ".pdf") = "" Then
with:
VBA Code:
        If Dir(FILE_PATH & ActiveCell.Value & "*.pdf") = "" Then
 
Upvote 0
Thanks for the reply BUT im an idiot & forgot to mention something.

Each customers name on the sheet im using to hyperlink has a ref code after it.

So still like the original post TOM JONES is actually TOM JONES 001

Returing customers are like so 002 or 003 or 004

Can you advise please.
Sorry for not mentioning in first post
 
Upvote 0
In that case the two changes to my post would be:

VBA Code:
Dim customerName As String
customerName = Left(ActiveCell.Value, InstrRev(ActiveCell.Value, " ") - 1)
file = Dir(FILE_PATH & customerName & "*.pdf")
If Len(file) Then
    ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & file

and

VBA Code:
        If Dir(FILE_PATH & customerName & "*.pdf") = "" Then
 
Upvote 0
Solution
Thanks,
Did i miss something as getting Variable not defined on this line.
VBA Code:
file = Dir(FILE_PATH & customerName & "*.pdf")

This is the code i have in use at present.

Code:
Private Sub HyperlinkDisco_Click()
        Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\"
        If ActiveCell.Column = Columns("B").Column Then
          
        Dim customerName As String
           customerName = Left(ActiveCell.Value, InStrRev(ActiveCell.Value, " ") - 1)
           file = Dir(FILE_PATH & customerName & "*.pdf")
        If Len(file) Then
           ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & file
           MsgBox "HYPERLINK WAS SUCCESSFUL.", vbInformation, "POSTAGE SHEET HYPERLINK MESSAGE"
        End If
        
        Else
           MsgBox "PLEASE SELECT A CUSTOMER FIRST TO HYPERLINK THE FILE.", vbCritical, "POSTAGE SHEET DISCO II HYPERLINK MESSAGE"
        Exit Sub
        End If
        
        If Dir(FILE_PATH & customerName & "*.pdf") = "" Then
        If MsgBox("THERE IS NO FILE FOR THIS CUSTOMER" & vbNewLine & "WOULD YOU LIKE TO OPEN THE DISCO II FOLDER ?", vbYesNo + vbCritical, "HYPERLINK CUSTOMER DISCO II MESSAGE.") = vbYes Then
           CreateObject("Shell.Application").Open ("C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\")
        End If

    
    End If
End Sub
 
Upvote 0
UPDATE
I removed Option Explicit at the top & it works
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top