Results 1 to 14 of 14
  1. #1
    Star Lounger
    Join Date
    Sep 2002
    Location
    New York, New York
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    test if Word document is already open in VB (Word 2000)

    I am using automation to manipulate Word from an Access database. The database has a function that will generate routine correspondence by merging information from the record into a template. The process takes a couple of seconds, during which time the letter is merged to a new document and then original template is closed and the new document is saved and its location and filename written back to the database.

    The document template is shared and resides on the server, and the problem comes when two people try to access the template at the same time (during that 5-8 second window while the template is open) and the "This document is locked for editing by another user..." dialogue appears. Is there a way for me to check whether or not the document is already open in the VB code and hence side-step this?

    thanks,
    Karl

  2. #2
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    I had an issue like this recently where we were creating invoices from data and using a shared .Dot template.

    In your code somewhere, you must have a line which points to the template on your network.
    Can't you give each user their own template in a local drive instead ?
    A few extra lines of code could check to see if they have the latest version if it changes.

  3. #3
    Star Lounger
    Join Date
    Sep 2002
    Location
    New York, New York
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    yeah, I've considered local copies as an option, but I really want there to be a cleaner solution with all of the templates in a single place. (as if wanting ever solved anything...)

    You say "a few extra lines of code could check to see if they have the latest version if it changes."

    Thinking this through, I would still keep a "master" copy on the network drive, and I would check the "modified" dates of the local and network copies... is this what you mean? If the copy wasn't current, could I automatically pull an updated version to the local drive? If so how?

    thanks,
    K

  4. #4
    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: test if Word document is already open in VB (W

    A template can be shared if all users access it using Documents.Add rather than Documents.Open -- at least, that's how it's supposed to work. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> You also could immediately detach the template from the new document if its use is purely stylistic and you don't need any further access to AutoText entries or macros in the template. Simply change AttachedTemplate to Normal.dot (I think the NormalTemplate object is the easiest way to make sure the path is correct but I haven't tested in forever).

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

    Re: test if Word document is already open in VB (W

    I think the problem is that they are actually changing the template.

    One thought is to Open the template directly, instead of a document based on the template

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

    Re: test if Word document is already open in VB (Word 2000)

    This function will return True if a file is open. It's up to you to check for other file errors.

    Function FileIsOpen(sFileName As String) As Boolean

    On Error GoTo FileError
    Open sFileName For Input Lock Read As 1
    On Error GoTo 0
    Close #1
    FileIsOpen = False

    Exit Function

    FileError:
    FileIsOpen = True

    End Function


    Regards,
    Kevin Bell

  7. #7
    Star Lounger
    Join Date
    Sep 2002
    Location
    New York, New York
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Thanks to all of you for your help. I now have enough info to solve the problem. Kevin's code is exactly what I was looking for. This work with automation is relatively new for me, so all assistance is greatly appreciated.
    best,
    Karl

  8. #8
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Sorry I did'nt come back to you on this, we've had a server change and lost some E:Mails.


    The code I use to copy a file from a network folder to a local folder looks something like below:
    Microsoft Scripting Runtime must be selected from project references.

    <pre> On Error GoTo errHandler
    Dim fso As New FileSystemObject

    Dim LocFile As String
    Dim SerFile As String

    LocFile = "CestinationFolderFileOrTemplate.dot"
    SerFile = "L:MMPDFEC-UpdateFileOrTemplate.dot"


    fso.DeleteFile LocFile
    fso.CopyFile SerFile, LocFile

    Set fso = Nothing
    Shell LocFile, vbNormalFocus
    Unload Me</pre>


  9. #9
    New Lounger
    Join Date
    Oct 2004
    Location
    Sydney, New South Wales, Australia
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Checkout Randy Birch Win32 website. It has far faster code for copying files.

    Pete

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Pete

    Do you have a link to his site ??

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Here it is: VBNet.

  12. #12
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    Cheers Hans

    There's some good stuff on there, I feel another update coming on !!

  13. #13
    New Lounger
    Join Date
    Oct 2004
    Location
    Sydney, New South Wales, Australia
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    don't be lazy....try

    Randy Birch in Google - the very first match is him !

    http://vbnet.mvps.org/

    Pete

  14. #14
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if Word document is already open in VB (Word 2000)

    whether the template is on a server or not should not matter. if you start a document from a (server)template with set mydoc=documents.add("myserverdocsmytemplate.dot"), you should not receive the error. still, if you're using the open method instead, you can include additional parameters that will prevent the warning from appearing.

Posting Permissions

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