Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a network file - testing for ReadOnly

Status
Not open for further replies.

davedave24

Programmer
Aug 26, 2010
113
GB
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

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
 
Looks like to check tee ReadOnly you either have to do this:
[tt]
If wWB[blue](1)[/blue].ReadOnly = True Then[/tt] (if you know the index of your workbook)

or - info from here -
[tt]
If ActiveWorkbook.ReadOnly Then
[/tt]

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top