Leith's approach lets you have good control.
For what you posted initially, just poking a value into a cell and getting it worked fine. I put one more routine in it that I use for text files sometimes.
What else did you need?
Sub Test_IsFileOpen()
Dim strPDFpath As String
Dim wbk As Workbook, ws As Worksheet
Range("A1").Value2 = ThisWorkbook.Path & "\" & GetBaseName(ThisWorkbook.FullName) & ".pdf"
strPDFpath = Range("A1").Value2
If IsFileOpen(strPDFpath) = True Then
MsgBox "File is open!", , strPDFpath
Else
MsgBox "File is not open!", , strPDFpath
End If
End Sub
Function IsFileOpen(FileName As String) As Variant
Dim FileNum As Integer, ErrNum As Integer
On Error Resume Next
FileNum = FreeFile()
Open FileName For Input Lock Read As #FileNum
Close FileNum
ErrNum = Err
On Error GoTo 0
Select Case ErrNum
Case 0
IsFileOpen = False
Case 70
IsFileOpen = True
Case Else
If ErrNum = 53 Then IsFileOpen = "Does Not Exit"
End Select
End Function
'RichardSchollar, http://www.ozgrid.com/forum/showthread.php?t=79132
Function IsFileWriteable(StrFilePath As String) As Boolean
Dim FileNum As Integer
IsFileWriteable = False
FileNum = FreeFile
On Error Resume Next
Open StrFilePath For Input Lock Read Write As #FileNum ' Open file and lock it.
If Err.Number = 0 Then IsFileWriteable = True 'Can write to file
Close FileNum
End Function
Function GetBaseName(filespec As String)
Dim fso As Object, s As String
Set fso = CreateObject("Scripting.FileSystemObject")
s = fso.GetBaseName(filespec)
Set fso = Nothing
GetBaseName = s
End Function
Bookmarks