• ReadOnly:=False is false (NL/2003)

    Author
    Topic
    #453445

    Hello All,

    In my code I use the following line:
    Workbooks.Open Filename:=strFileName, ReadOnly:=False, UpdateLinks:=False
    And allthough I say ReadOnly:=False, if the workbook is already open by another user it is opened as ReadOnly.
    Is there a way to check if the book is opened with the ReadOnly status to True, because then I can show a message that the update will not work and close the workbook

    Thanks inadvance for your reactions.

    Viewing 1 reply thread
    Author
    Replies
    • #1122972

      You can use this function to test:

      Function IsFileOpen(FileName As String) As Boolean
      Dim f As Integer
      Dim intErr As Integer

      On Error Resume Next ‘ Turn error checking off.
      f = FreeFile
      ‘ Attempt to open the file and lock it.
      Open FileName For Input Lock Read As #f
      intErr = Err ‘ Save the error number that occurred.
      Close f ‘ Close the file.

      ‘ Check to see which error occurred.
      Select Case intErr
      Case 0
      ‘ No error occurred.
      ‘ File is NOT already open by another user.
      Case 70
      ‘ Error number for “Permission Denied.”
      ‘ File is already opened by another user.
      IsFileOpen = True
      Case Else
      ‘ Another error occurred.
      Error intErr
      End Select
      End Function

      Example of use:

      If IsFileOpen(strFileName) Then
      MsgBox “Workbook is already in use.”, vbExclamation
      Else
      Workbooks.Open …
      End If

      By the way, were the replies to post 723,237 and post 724,311 useful? You didn’t provide any feedback.

    • #1123091

      You can test for the readonly mode:

      If Workbooks(“Foo.xls”).ReadOnly Then
      ‘Book is in readonly mode
      End If

      • #1126065

        Hans and Jan Karel,
        I’m sure that the solution of Hans will work, but i’m using the short version of Jan Karel.
        It works perfect.
        Thanks.

    Viewing 1 reply thread
    Reply To: ReadOnly:=False is false (NL/2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: