Consulting

Results 1 to 5 of 5

Thread: how to check if file is read only, but from other file

  1. #1

    how to check if file is read only, but from other file

    hi all,
    how can i check follow:
    I have opened file A
    And after run code i want to check if is file B opened read only?

    i tried this, but doesnt works:

    set target = Workbooks.Open FileName:= "Y:\path\aa\bb\B", UpdateLinks:=0
     
    If target.ReadOnly = True Then MsgBox "read-only"
    maybe i need define instead thisworkbook to path to file B?
    thx a lot

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This should work

    [vba]

    set target = Workbooks.Open(FileName:= "Y:\path\aa\bb\B", UpdateLinks:=0)

    If target.ReadOnly = True Then MsgBox "read-only"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    this open a file B and check it

    but is it possible without opening the file B?
    maybe not or?
    thx

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not as stated as Readonly only appies to workbooks open.

    You can check if the file is alraedy in use with

    [vba]

    Function IsFileOpen(FileName As String)
    Dim iFilenum As Long
    Dim iErr As Long

    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0

    Select Case iErr
    Case 0: IsFileOpen = False
    Case 70: IsFileOpen = True
    Case Else: Error iErr
    End Select

    End Function

    Sub test()
    If Not IsFileOpen("C:\MyTest\volker2.xls") Then
    Workbooks.Open "C:\MyTest\volker2.xls"
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    OK it works, thank you...and is possible to figured out, who opened this file. I mean username of person whos open this file?
    Last edited by danovkos; 11-12-2009 at 08:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •