Check if file extsts before continue

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I am using the code below but would like to check if file already exists.
Basically if file exists stop & show Msgbox advising user
If the file does not exist then continue


Currently nothing is in place & is just written over each time

VBA Code:
Private Sub HyperlinkDiscoII_Click()
Dim wb As Workbook
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"
        With ActiveCell
        .Font.Size = 12
        End With
        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
       Unload DiscoIIHyperlink
       Workbooks("DISCO CALC.xlsm").Close SaveChanges:=True
    
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
see if this helps:
VBA Code:
Function fileExists(strfilename As String) As Boolean
    'Requires reference to Microsoft Scripting Runtime
    Dim fso As New FileSystemObject
    fileExists = fso.fileExists(strfilename)
    Set fso = Nothing
End Function

or the Late binding version:
VBA Code:
Function fileExists(strfilename As String) As Boolean
    With CreateObject("Scripting.FileSystemObject")
        fileExists = .fileExists(strfilename)
    End With
End Function
 
Last edited:
Upvote 0
That code already does that with Dir.

Hmmm

I can mouse over to that cell & run the code & i see HYPERLINK SUCCESSFUL etc.
I dont get any message saying file exists or the like hence why i started this post
 
Upvote 0
I dont get any message saying file exists or the like hence why i started this post
what means is that an actual check(s) for the existence of a file is performed:
VBA Code:
Len(Dir(FILE_PATH & ActiveCell.Value & ".pdf"))
the result will be a number > 0 if the file exists
VBA Code:
Dir(FILE_PATH & ActiveCell.Value & ".pdf") = ""
this will return TRUE if the the file does not exist.

How you use this results in your code is a separate matter.

And in your code I actually see no file writing.
 
Upvote 0
Well im now confused.
The code i supplied hyperlinks a pdf file.

So i select the customer on the worksheet say TOM JONES 001
The code is run.
I see the message Hyperlink Successful.
Now the customers name is Blue & underlined.
Clicking on it will open the pdf file in the folder where they are stored.

My goal was should a user select or mouse to that cell where TOM JONE 001 is located & code is run THEN advise user.

Did i word it incorrectly & should of said check to see if the pdf is already hyperlinked ?

Sorry
 
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