Detect if a PDF is open or not

David Ball

Registered User.
Local time
Today, 11:57
Joined
Aug 9, 2010
Messages
230
Hi,

The code below opens a PDF selected from a form. Whatever document number appears in the textbox (FICNo) on the form is the document that is opened when the command button is clicked.
It works but I would like to add a message box to say “File not found” if the document number in the textbox has no corresponding document in the folder (ScannedFiles).

Code:
[FONT=Calibri][SIZE=3]Option Explicit[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Option Compare Database[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Private Const SW_SHOWNORMAL As Long = 1[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Private Const SW_SHOWMAXIMIZED As Long = 3[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Private Sub CmdOpenDoc_Click()[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]ShellExecute 0, "Open", "C:\Users\David.Ball\Desktop\ScannedFiles\" & Me.FICNo & ".pdf", vbNullString, "C:\", SW_SHOWNORMAL[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]End Sub [/SIZE][/FONT]


How can I modify the code to do this?
Thanks very much
Dave
 
Not sure, but I think that the ShellExecute command will raise a error if the file don't exist.
So use a Error Handler (OnError statement) in order to catch the error and to show the message.
 
On way is to check if the file exists before trying to open it. I haven;t checked this code, as I'm on my tablet, so double check the syntax
..
Code:
If Len(Dir("C:\Users\David.Ball\Desktop\ScannedFiles\" & Me.FICNo & ".pdf")) = 0 Then
MsgBox"Sorry File Doesn't Exisit"
Else
Rest of your code here....
 

Users who are viewing this thread

Back
Top Bottom