davedave24
Programmer
Hi all
I need my userform to open an additional file for input. The file is on a network drive, so I need to perform the following checks:
1) the current user hasn't already got the file open in the background (this works)
2) another user hasn't got the file open (which would open in readonly)
Here is what I have. I'm getting a Subscript error on the readonly check
I need my userform to open an additional file for input. The file is on a network drive, so I need to perform the following checks:
1) the current user hasn't already got the file open in the background (this works)
2) another user hasn't got the file open (which would open in readonly)
Here is what I have. I'm getting a Subscript error on the readonly check
Code:
Dim wFilePath As String
Dim wFileName As String
Dim wWB As Workbook
wFilePath = "\\MAIN-PC\Users\Public\"
wFileName = "TESTFILE.xlsx"
'check if this user has the workbook open already
If FileInUse(wFilePath & wFileName) Then
Set wWB = Workbooks("TESTFILE.xlsx")
Else
Set wWB = Workbooks.Open(wFilePath & wFileName)
End If
If wWB.ReadOnly = True Then
MsgBox "file is readonly"
wWB.Close False
End If
Public Function FileInUse(sFileName) As Boolean
On Error Resume Next
Open sFileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
FileInUse = True
Err.Clear
End If
End Function