2

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
Mayukh Bhattacharya
  • 12,541
  • 5
  • 21
  • 32
  • Your `mypath` variable has a filename in it, so when you pass it to `FileExists` is concatenating the filename again at the end of the filename that is in the `mypath` variable - and of course, that file does not exist. It would look something like this: `"E:\4-2022\test.pdftest.pdf"` – braX May 22 '23 at 00:25
  • So from the file exist funtion should I remove the ".pdf"? – Funny Memo Ms May 22 '23 at 00:38
  • thank you worked when I removed it, I have 3 macros with the same code but different folders. should the FileExist function filename and path change? like path1 and filename1? forgive my other question but related to same code. – Funny Memo Ms May 22 '23 at 00:41
  • Intead of `If Not FileExists(mypath, filename) Then` use `If Dir(mypath) = "" Then` - and then you won't need that function at all. – braX May 22 '23 at 00:45
  • `On Error Resume Next` hides errors so you cannot fix them. https://stackoverflow.com/questions/31753201/vba-how-long-does-on-error-resume-next-work/31753321#31753321 "you should almost NEVER use it. You should figure out why the error occurs and code to handle it." – niton May 22 '23 at 01:32

1 Answers1

2

View Document

Option Explicit

Sub ViewDoc1() ' improve e.g. 'ViewLedger'!
    ViewDocument "E:\4-2022", frmDELEGATION.txtLedger.Value
End Sub

Sub ViewDocument( _
        ByVal FolderPath As String, _
        ByVal FileBaseName As String, _
        Optional ByVal FileExtension As String = ".pdf")
    
    Const PROC_TITLE As String = "View Document"

    If Right(FolderPath, 1) <> Application.PathSeparator Then
        FolderPath = FolderPath & Application.PathSeparator
    End If
    
    If Len(Dir(FolderPath, vbDirectory)) = 0 Then
        MsgBox "The path """ & FolderPath & """ doesn't exist.", _
            vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    If Left(FileExtension, 1) <> "." Then
        FileExtension = "." & FileExtension
    End If
    
    Dim fName As String: fName = FileBaseName & FileExtension
    Dim fPath As String: fPath = FolderPath & fName
 
    If Len(Dir(fPath)) = 0 Then
        MsgBox "The file """ & fName & """ was not found in the folder """ _
            & FolderPath & """.", vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    On Error Resume Next
        ThisWorkbook.FollowHyperlink fPath
    On Error GoTo 0

End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28