Below is the code I use to open a pdf file. - 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.
- Public Function FileExist(FilePath As String) As Boolean
-
FileExist = (Dir(FilePath) <> "")
-
End Function
7 2842
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.
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)".
Create a FileSystemObject. That object has a function called FileExists("path to file").
- Public Function FileExist(FilePath As String) As Boolean
-
FileExist = (Dir(FilePath) <> "")
-
End Function
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 : - '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.
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 - 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. - 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. - 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
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
|
by: cfyam |
last post by:
How can I check some file is exist or not in my evc dll?
|
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.
...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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??
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |