Yes, you can add this into your code:
Filename = ThisWorkbook.Path & "\" & sName & ".pdf"
FileCheck (Filename)
Select Case FileCheck(Filename)
Case 1
MsgBox ("The file " & Filename & " exist")
'your code
Case -1
MsgBox ("The file " & Filename & " is already open")
'your code
Case 0
MsgBox ("The file " & Filename & " is not in current directory")
'so you can put the rest of your code here
End Select
and below your macro:
Function FileCheck(ByVal sFull As String) As Integer
'returnvalue:
' -1 is open
' 1 is ondisk
' 0 is not on disk/error
Dim iRes%, sName$
Dim wb As Workbook
sName = Dir(sFull)
iRes = -(Len(sName) > 0)
If iRes = 1 Then
For Each wb In Application.Workbooks
If StrComp(wb.Name, sName, vbTextCompare) = 0 Then
iRes = -1
Exit For
End If
Next
End If
FileCheck = iRes
End Function
Cheers
Erwin
Bookmarks