How to know if a pdf file does not exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rick Beach
    New Member
    • Jan 2011
    • 25

    How to know if a pdf file does not exist

    Below is the code I use to open a pdf file.

    Code:
    1.)Dim stAppName As String
    2.)    Dim varFile As String
    3.)    Dim varFile2 As String
    4.)    
    5.)If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
    6.)
    7.)MsgBox "There is no Job Plan associated with this record."
    8.)
    9.)Else
    10.)
    11.)varFile = Me.JobPlanFolder
    12.)
    13.)stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile
    14.)    
    15.)Call Shell(stAppName, 1)
    16.)    
    17.)varFile2 = Me.JobPlanFolder2
    18.)    
    19.)stAppName2 = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile2
    20.)
    21.)    Call Shell(stAppName2, 1)
    22.)End If
    This works correctly but I need a little assistance. If the first file does not exist, I do not want to see the file not found error message and search for the second file. If the file does exist, I do not want to search for the second file.
    Last edited by Rabbit; Mar 21 '12, 08:30 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    If you don't want to see the error message, then take out the error message. To search for the second file, do the same thing that you do to search for the first file.

    Comment

    • Rick Beach
      New Member
      • Jan 2011
      • 25

      #3
      The msgbox you are referring to is if there is no pdf file name. This message box I need. The two files being opened after the else statement is where I need assistance. I need to see if the Me.JobPlanFolde r (This contains the file path and file name) exists before running the "Call Shell(stAppName , 1)". If the Me.JobPlanFolde r does not exist I need to run the "Call Shell(stAppName 2, 1)".

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Create a FileSystemObjec t. That object has a function called FileExists("pat h to file").

        Comment

        • Mihail
          Contributor
          • Apr 2011
          • 759

          #5
          Code:
          Public Function FileExist(FilePath As String) As Boolean
              FileExist = (Dir(FilePath) <> "")
          End Function

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            That's a good answer Mihail, but I'd make a few minor amendments to handle file properties such as Read Only; System; Hidden; etc :
            Code:
            'Exist() returns true if strFile exists.  By default ignores folders, but handles if required.
            Public Function Exist(strFile As String, _
                                  Optional intAttrib As Integer = vbReadOnly Or _
                                                                  vbHidden Or _
                                                                  vbSystem) As Boolean
                Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
            End Function
            @Rick
            Please see Before Posting (VBA or SQL) Code to avoid this sort of mess in future.
            Last edited by NeoPa; Mar 29 '12, 02:17 PM. Reason: Updated procedure code.

            Comment

            • Hennepin
              New Member
              • Oct 2009
              • 25

              #7
              The code will work correctly until you have a differnt version of Adobe reader or it is not installed in the default place.
              Using your method of opening a pdf.
              You could check the return on shell and suppress the error message from shell
              Code:
              Dim varFile As String
              Dim varFile2 As String
              Dim retval
              On Error Resume Next ' prevent error message from Shell
              If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
                  MsgBox "There is no Job Plan associated with this record."
              Else
                  varFile = Me.JobPlanFolder
                  stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile
                  retval = Shell(stAppName, 1) ' returns number greater than 0 if succeeds
                  If retval <> 0 Then
                      varFile2 = Me.JobPlanFolder2
                      stAppName2 = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile2
                      retval = Shell(stAppName2, 1)
                  End If
              End If
              Or use an API call and have the operating system open the file. Place the following code in a module. This api code will open any file your system knows how to open.
              Code:
              Option Compare Database
              Option Explicit
              
              '************ Code Start **********
              Private Declare Function apiShellExecute Lib "Shell32.dll" _
                  Alias "ShellExecuteA" _
                  (ByVal hwnd As Long, _
                  ByVal lpOperation As String, _
                  ByVal lpFile As String, _
                  ByVal lpParameters As String, _
                  ByVal lpDirectory As String, _
                  ByVal nShowCmd As Long) _
                  As Long
              '***Error Codes***
              Private Const ERROR_SUCCESS = 32&
              Private Const ERROR_NO_ASSOC = 31&
              Private Const ERROR_OUT_OF_MEM = 0&
              Private Const ERROR_FILE_NOT_FOUND = 2&
              Private Const ERROR_PATH_NOT_FOUND = 3&
              Private Const ERROR_BAD_FORMAT = 11&
              
              '***************Usage Examples***********************
              'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
              'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
              'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
              'Handle Unknown extensions (call Open With Dialog):
              '                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
              'Start Access instance:
              '                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
              '****************************************************
              Public Function fHandleFile(stFile As String, lShowHow As VbAppWinStyle)
                  Dim lRet As Long, varTaskID As Variant
                  Dim stRet As String
                  'First try ShellExecute
                  lRet = apiShellExecute(hWndAccessApp, vbNullString, _
                          stFile, vbNullString, vbNullString, lShowHow)
                          
                  If lRet > ERROR_SUCCESS Then
                      stRet = vbNullString
                      lRet = -1
                  Else
                      Select Case lRet
                          Case ERROR_NO_ASSOC:
                              'Try the OpenWith dialog
                              varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & stFile, vbNormalFocus)
                              lRet = (varTaskID <> 0)
                          Case ERROR_OUT_OF_MEM:
                              stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
                          Case ERROR_FILE_NOT_FOUND:
                              stRet = "Error: File not found.  Couldn't Execute!"
                          Case ERROR_PATH_NOT_FOUND:
                              stRet = "Error: Path not found. Couldn't Execute!"
                          Case ERROR_BAD_FORMAT:
                              stRet = "Error:  Bad File Format. Couldn't Execute!"
                          Case Else:
                      End Select
                  End If
                  fHandleFile = lRet & IIf(stRet = "", vbNullString, ", " & stRet)
              End Function
              Then your code would look like this.
              Code:
              Dim varFile As String
              Dim varFile2 As String
              Dim estring As Variant
              If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
                  MsgBox "There is no Job Plan associated with this record."
              Else
                  varFile = Me.JobPlanFolder
                  estring = fHandleFile(varFile, vbNormalFocus)
                  If estring = "-1" Then
                      varFile2 = Me.JobPlanFolder2
                      fHandleFile varFile2, vbNormalFocus
                  End If
              End If

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                I don't mean to be antagonistic, but surely the OP needs only some simple code to determine the existence of a particular file (Which working code has already been suggested in a few lines). I cannot see any reason to post a suggestion comprising many tens of lines (as well as being many times more involved than necessary).

                Comment

                Working...