• Testing for File In Use (Word 97SR2)

    Author
    Topic
    #365426

    If you try to open a document that is already in use by another user, Word will respond with the in use dialog (see below). I would like to be able to test for FileInUse without the user seeing any message or message box. How can I do that?

    Testing for the presence of the temporary file of the open file is unreliable. I need a better way.

    Viewing 1 reply thread
    Author
    Replies
    • #564012

      Try On Error REsume Next and test the error code.
      Might do the deed.

    • #564117

      Kev,

      Try this:

      boolFileInUse = True
      On error goto FileInUse
      Open “The file you want to open” for input lock read as 1
      on error goto 0
      close #1
      boolFileInUse=false
      FileInUse:

      etc.

      Cheers,

      Kevin Bell

      • #564209

        Touch

      • #564458

        Talk about embarassing! bagged

        I actually had this code in my app!
        I forget where I got it, but it does the job by another name.

        Function fFileLocked(strFileName As String) As Boolean
        On Error Resume Next
        Open strFileName For Binary Access Read Write Lock Read Write As #1
        Close #1
        If Err.Number 0 Then
        fFileLocked = True
        Else
        fFileLocked = False
        End If
        Err.Clear
        End Function

        • #564539

          Hi Kevin,

          Also in the ‘seen it before’ category, couldn’t you replace

          If Err.Number 0 Then
          fFileLocked = True
          Else
          fFileLocked = False
          End If

          with

          fFileLocked = CBool(Err.Number)

          Gary

          • #564644

            You are a very good student!

          • #564701

            While were at it, why not reiterate the idea that since the Function is declared Boolean the assignment statement does not need the CBool function. Therefore,

            fFileLocked = Err.Number

            will suffice. yawn

            • #564747

              Edited by Gary Frieder on 18-Jan-02 19:09.

              Actually that’s what I was going to post, and then got cold feet. I thought: even though I know it’s going to work, would I release that code? So I added the CBool back as a security blanket, even though not really necessary.

              Kind of like setting a boolean variable = False at the beginning of a function – not really necessary but one sleeps better.

              Still, what you’ve posted wins the brevity prize. ribbon trophy

            • #564748

              Where’s my ribbon?

        • #564581

          That’s what I had suggested.

          • #564584

            It’s a little different in that he is not trapping the error when the .Open method fails, but separately opening and closing the file using old VB as a test before running the .Open method. I’m not sure it’s worth the extra code to do it that way, but since it’s already written…

    Viewing 1 reply thread
    Reply To: Testing for File In Use (Word 97SR2)

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

    Your information: