473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to know if a pdf file does not exist

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

7 2842
Rabbit
12,516 Expert Mod 8TB
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
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
12,516 Expert Mod 8TB
Create a FileSystemObject. That object has a function called FileExists("path to file").
Mar 21 '12 #4
Mihail
759 512MB
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
32,556 Expert Mod 16PB
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
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
32,556 Expert Mod 16PB
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

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

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
by: cfyam | last post by:
How can I check some file is exist or not in my evc dll?
0
by: netjanuszk7 | last post by:
In SQL Server 2000 I have created a linked server. It was to link me with an Excel file. I received a message: I know about system.mdw, but it is linked with Access. And what about Excel. ...
1
by: TrailBlazer | last post by:
Hi - Been struggling over this issue for a long while. We store PDF's in our SQL Server database. And I do a BinaryWrite to display the PDF. It works for me without issue, on FireFox, IE, and...
3
by: OdAwG | last post by:
Hello All, I would i check to see if a file exist and if it does exist, then rename the file with the date and time attached to the name: Example: If...
1
by: prabhunew2005 | last post by:
Hi I am doing web portal design using PHP. The system has linux os. When i am moving to any of screens, the following error message is written into error log file. File does not exist:...
1
by: Lloyd Dupont | last post by:
I think you could test that for your self, couldn't you! ;-) VS is a good hexa editor, if you want to do some hexa read of your file... "DR" <softwareengineer98037@yahoo.comwrote in message...
23
by: canabatz | last post by:
can someone please help me set up a loop to wait for a file to be exist? i got a automation program that download a file from my ftp ,proccessing the file and returning back the result as a...
11
by: Abhilash Etikala | last post by:
File does not exist: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/favicon.ico why i am getting this error??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.