I am trying to view a pdf document with name of file in a textbox and a button to view this file.
When the pdf file is in the path it opens.
When it is not there when I click the button nothing happens.
I need to add a msgbox to pop out if the pdf file is not found.
I added File exist function. I don't know if it is the correct method or if there is an easier way to add a msgbox to hyperlink if file is not there.
The problem with my code is that:
If the file is there, it opens normally but shows the msgbox "No Documents Found."
If the file is not there it shows the msgbox "No Documents Found." as well.
Sub viewdoc()
On Error Resume Next
Dim mypath As String
Dim filename As String
filename = frmDELEGATION.txtLedger.Value
mypath = "E:\4-2022\" & filename & ".pdf"
ThisWorkbook.FollowHyperlink mypath
If Not FileExists(mypath, filename) Then
MsgBox "No Documents Found ."
Exit Sub
End If
End Sub
Function FileExists(ByVal mypath As String, ByVal filename As String) As Boolean
FileExists = (Dir(mypath & filename & ".pdf") <> "")
End Function