By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,773 Members | 2,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,773 IT Pros & Developers. It's quick & easy.

How to know if a pdf file does not exist

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

Expand|Select|Wrap|Line Numbers
  1. 1.)Dim stAppName As String
  2. 2.)    Dim varFile As String
  3. 3.)    Dim varFile2 As String
  4. 4.)    
  5. 5.)If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
  6. 6.)
  7. 7.)MsgBox "There is no Job Plan associated with this record."
  8. 8.)
  9. 9.)Else
  10. 10.)
  11. 11.)varFile = Me.JobPlanFolder
  12. 12.)
  13. 13.)stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile
  14. 14.)    
  15. 15.)Call Shell(stAppName, 1)
  16. 16.)    
  17. 17.)varFile2 = Me.JobPlanFolder2
  18. 18.)    
  19. 19.)stAppName2 = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile2
  20. 20.)
  21. 21.)    Call Shell(stAppName2, 1)
  22. 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.
Mar 21 '12 #1

✓ answered by Mihail

Expand|Select|Wrap|Line Numbers
  1. Public Function FileExist(FilePath As String) As Boolean
  2.     FileExist = (Dir(FilePath) <> "")
  3. End Function

Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,356
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.
Mar 21 '12 #2

P: 25
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.JobPlanFolder (This contains the file path and file name) exists before running the "Call Shell(stAppName, 1)". If the Me.JobPlanFolder does not exist I need to run the "Call Shell(stAppName2, 1)".
Mar 21 '12 #3

Rabbit
Expert Mod 10K+
P: 12,356
Create a FileSystemObject. That object has a function called FileExists("path to file").
Mar 21 '12 #4

100+
P: 759
Expand|Select|Wrap|Line Numbers
  1. Public Function FileExist(FilePath As String) As Boolean
  2.     FileExist = (Dir(FilePath) <> "")
  3. End Function
Mar 22 '12 #5

NeoPa
Expert Mod 15k+
P: 31,417
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 :
Expand|Select|Wrap|Line Numbers
  1. 'Exist() returns true if strFile exists.  By default ignores folders, but handles if required.
  2. Public Function Exist(strFile As String, _
  3.                       Optional intAttrib As Integer = vbReadOnly Or _
  4.                                                       vbHidden Or _
  5.                                                       vbSystem) As Boolean
  6.     Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  7. End Function
@Rick
Please see Before Posting (VBA or SQL) Code to avoid this sort of mess in future.
Mar 22 '12 #6

P: 25
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
Expand|Select|Wrap|Line Numbers
  1. Dim varFile As String
  2. Dim varFile2 As String
  3. Dim retval
  4. On Error Resume Next ' prevent error message from Shell
  5. If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
  6.     MsgBox "There is no Job Plan associated with this record."
  7. Else
  8.     varFile = Me.JobPlanFolder
  9.     stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile
  10.     retval = Shell(stAppName, 1) ' returns number greater than 0 if succeeds
  11.     If retval <> 0 Then
  12.         varFile2 = Me.JobPlanFolder2
  13.         stAppName2 = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe " & varFile2
  14.         retval = Shell(stAppName2, 1)
  15.     End If
  16. End If
  17.  
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.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. '************ Code Start **********
  5. Private Declare Function apiShellExecute Lib "Shell32.dll" _
  6.     Alias "ShellExecuteA" _
  7.     (ByVal hwnd As Long, _
  8.     ByVal lpOperation As String, _
  9.     ByVal lpFile As String, _
  10.     ByVal lpParameters As String, _
  11.     ByVal lpDirectory As String, _
  12.     ByVal nShowCmd As Long) _
  13.     As Long
  14. '***Error Codes***
  15. Private Const ERROR_SUCCESS = 32&
  16. Private Const ERROR_NO_ASSOC = 31&
  17. Private Const ERROR_OUT_OF_MEM = 0&
  18. Private Const ERROR_FILE_NOT_FOUND = 2&
  19. Private Const ERROR_PATH_NOT_FOUND = 3&
  20. Private Const ERROR_BAD_FORMAT = 11&
  21.  
  22. '***************Usage Examples***********************
  23. 'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
  24. 'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
  25. 'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
  26. 'Handle Unknown extensions (call Open With Dialog):
  27. '                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
  28. 'Start Access instance:
  29. '                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
  30. '****************************************************
  31. Public Function fHandleFile(stFile As String, lShowHow As VbAppWinStyle)
  32.     Dim lRet As Long, varTaskID As Variant
  33.     Dim stRet As String
  34.     'First try ShellExecute
  35.     lRet = apiShellExecute(hWndAccessApp, vbNullString, _
  36.             stFile, vbNullString, vbNullString, lShowHow)
  37.  
  38.     If lRet > ERROR_SUCCESS Then
  39.         stRet = vbNullString
  40.         lRet = -1
  41.     Else
  42.         Select Case lRet
  43.             Case ERROR_NO_ASSOC:
  44.                 'Try the OpenWith dialog
  45.                 varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & stFile, vbNormalFocus)
  46.                 lRet = (varTaskID <> 0)
  47.             Case ERROR_OUT_OF_MEM:
  48.                 stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
  49.             Case ERROR_FILE_NOT_FOUND:
  50.                 stRet = "Error: File not found.  Couldn't Execute!"
  51.             Case ERROR_PATH_NOT_FOUND:
  52.                 stRet = "Error: Path not found. Couldn't Execute!"
  53.             Case ERROR_BAD_FORMAT:
  54.                 stRet = "Error:  Bad File Format. Couldn't Execute!"
  55.             Case Else:
  56.         End Select
  57.     End If
  58.     fHandleFile = lRet & IIf(stRet = "", vbNullString, ", " & stRet)
  59. End Function
Then your code would look like this.
Expand|Select|Wrap|Line Numbers
  1. Dim varFile As String
  2. Dim varFile2 As String
  3. Dim estring As Variant
  4. If Right(Me.JobPlanFolder, 5) = "\.pdf" Then
  5.     MsgBox "There is no Job Plan associated with this record."
  6. Else
  7.     varFile = Me.JobPlanFolder
  8.     estring = fHandleFile(varFile, vbNormalFocus)
  9.     If estring = "-1" Then
  10.         varFile2 = Me.JobPlanFolder2
  11.         fHandleFile varFile2, vbNormalFocus
  12.     End If
  13. End If
Mar 22 '12 #7

NeoPa
Expert Mod 15k+
P: 31,417
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).
Mar 23 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.