Results 1 to 12 of 12
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Testing for File In Use (Word 97SR2)

    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.
    Attached Images Attached Images
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Testing for File In Use (Word 97SR2)

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

  3. #3
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Testing for File In Use (Word 97SR2)

    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

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Testing for File In Use (Word 97SR2)

    Touch
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I Knew I'd seen it before

    Talk about embarassing! <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

    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
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: I Knew I'd seen it before

    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

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: I Knew I'd seen it before

    That's what I had suggested.

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: I Knew I'd seen it before

    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...

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: I Knew I'd seen it before

    You are a very good student!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: I Knew I'd seen it before

    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. <img src=/S/yawn.gif border=0 alt=yawn width=15 height=15>
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: I Knew I'd seen it before

    Where's my ribbon?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: I Knew I'd seen it before

    <P ID="edit" class=small>Edited by Gary Frieder on 18-Jan-02 19:09.</P>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. <img src=/S/ribbon.gif border=0 alt=ribbon width=15 height=15> <img src=/S/trophy.gif border=0 alt=trophy width=15 height=15>

Posting Permissions

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