Results 1 to 12 of 12
  1. #1
    Star Lounger
    Join Date
    Apr 2002
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check whether if a template is open(active) or not (Word 2000)

    I have the following macros AutoNew, AutoOpen, AutoClose and AutoExit in a template. When users open/close/start a new document in the template, some files are copied to specific directory. When users exit the template, some of the files are deleted. Because of that, I can't not have multiple sessions active of that template. I can only have one at a time. I want to know if there is a way I can check to see if the template is open/active, if so don't do anything. Here is what I want

    let call the template x

    if x is not already open or active then
    {execute autoclose, autoopen, autoexit, autonew as mentioend above)

    I want to put my autonew, autoopen, autoexit, autoclose inside an if statement.

  2. #2
    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: Check whether if a template is open(active) or not (Word 2000)

    There have been some threads on the VB/VBA board about determining whether a file is in use by someone else. I don't know if those methods will work for a template, but you might try reviewing those threads and seeing if they help.

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    You could try cycling through the documents collection looking to see if any other document is using the particular template, something like...

    <pre>Dim doc As Document
    For Each doc In Application.Documents
    <font color=448800>' Ignore the current document</font color=448800>
    If doc.Name <> ActiveDocument.Name Then
    <font color=448800>' Check to see if this document uses our template</font color=448800>
    If doc.AttachedTemplate.Name = ActiveDocument.AttachedTemplate.Name Then
    MsgBox doc.Name
    End If
    End If
    Next doc
    End Sub
    </pre>


    StuartR

  4. #4
    Star Lounger
    Join Date
    Apr 2002
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    This works, I will work on it to make it the way I want

    thanks

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

    Re: Check whether if a template is open(active) or not (Word 2000)

    This is not really an issue of whether the template is being used, rather it is an issue of the macros in the template keeping track of what files have been written/deleted.

    If the files copied to the specific directory can be shared by users, the template can keep track of whether it needs to write the files.

    If the files cannot be shared, then the template can "gracefully" skip processing the appropriate code and close the document, telling the user to try again later.

  6. #6
    Star Lounger
    Join Date
    Apr 2002
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    Currently the code does the opposite. I have been trying to rework the if statemetn without success. All that I need for example, when I first open the templage or if the themplate is in use. The file should not be copy to that directory. However if the templage is not in use, the file should be copied. With the code below, it does the Opposite. 'When I open a templage create a document, not file is copied. If I open a second one, a file is copied. I don't want that. I want the file to copy, only if I open a first one. For the second one, no copy.

    Dim Doc As Document

    For Each Doc In Application.Documents
    If Doc.Name <> ActiveDocument.Name Then
    If Doc.AttachedTemplate.Name = ActiveDocument.AttachedTemplate.Name Then
    ' MsgBox (Doc.Name)
    End If
    FileCopy "c:datatest.txt", "c:data2test.txt"
    End If

    Next Doc

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    You need to check every document to see if any one of them is using your template, you can't include your code in the middle of the loop I showed you because you haven't finished checking at that stage. Try this...
    <pre>Dim doc As Document
    Dim blFirst As Boolean

    blFirst = True
    For Each doc In Application.Documents
    If doc.Name <> ActiveDocument.Name Then
    If doc.AttachedTemplate.Name = ActiveDocument.AttachedTemplate.Name Then
    blFirst = False
    End If
    End If
    Next doc

    If blFirst Then
    <font color=448800>' Put your code here</font color=448800>
    End If
    </pre>

    StuartR

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

    Re: Check whether if a template is open(active) or not (Word 2000)

    Another way to do this, which requires some rearranging of things but is fun to play around with, is to create your own 'pseudo- object reference counter':

    You need to have a global template in the Startup directory.
    In the declarations section of a standard code module in the global template, put:

    <pre>Public glngOpenDocsBasedOnTemplateCt As Long</pre>

    - this will initialize to a value of 0 (zero) every time Word is launched.

    Then in your workgroup template:
    (1) Set a reference (Tools>References) to your global template.
    (2) put code like this into the ThisDocument module of the workgroup template:

    <pre>Private Sub Document_Close()
    If ActiveDocument.Type <> wdTypeTemplate Then
    If glngOpenDocsBasedOnTemplateCt > 0 Then
    glngOpenDocsBasedOnTemplateCt = glngOpenDocsBasedOnTemplateCt - 1
    End If
    End If
    End Sub
    '==================
    Private Sub Document_New()
    If glngOpenDocsBasedOnTemplateCt > 0 Then
    MsgBox "Another document based on this template is already open"
    Else
    MsgBox "This the first document based on this template"
    End If
    glngOpenDocsBasedOnTemplateCt = glngOpenDocsBasedOnTemplateCt + 1
    End Sub
    '==================
    Private Sub Document_Open()
    If ActiveDocument.Type <> wdTypeTemplate Then
    If glngOpenDocsBasedOnTemplateCt > 0 Then
    MsgBox "Another document based on this template is already open"
    Else
    MsgBox "This the first document based on this template"
    End If
    glngOpenDocsBasedOnTemplateCt = glngOpenDocsBasedOnTemplateCt + 1
    End If
    End Sub
    </pre>

    You would, of course, need a differently named global counter variable, for each different template for which you wanted this capability.
    <img src=/S/cool.gif border=0 alt=cool width=15 height=15>
    Gary

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    Gary,

    I you're going to use this method then it might be easier to use a Static variable within a Function in the Template itself. It only needs to be accessible to documents based on that template, so should be accessible all the time.

    (Untested example...)
    <pre>Private Sub Document_New()
    If DocCount(1) > 1 then
    MsgBox "Another document based on this template is already open"
    Else
    MsgBox "This the first document based on this template"
    End If
    End Sub
    ===============
    Private Sub Document_Open()
    If DocCount(1) > 1 then
    MsgBox "Another document based on this template is already open"
    Else
    MsgBox "This the first document based on this template"
    End If
    End Sub
    ===============
    Private Sub Document_Close()
    DocCount(-1)
    End Sub
    ===============
    Private Function DocCount(iIncrement as integer) as Integer
    Static iNumberOfDocs as integer
    iNumberOfDocs = iNumberOfDocs + iIncrement
    DocCount = iNumberOfDocs
    End Function
    </pre>


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

    Re: Check whether if a template is open(active) or not (Word 2000)

    Hi Stuart,

    There may be the kernel of something there, but when I try it I get the "this is first document" message box every time. Have to admit I haven't worked with static variables, but I think what's happening there is that the value of the static variable is retained as long as the code in that module is running; once the code stops running, the static variable is destroyed (I think), even though the project is still open.

    Putting a MsgBox like this into the function, will always return "0":

    <pre>Private Function DocCount(iIncrement As Integer) As Integer
    Static iNumberOfDocs As Integer
    MsgBox CStr(iNumberOfDocs)
    iNumberOfDocs = iNumberOfDocs + iIncrement
    DocCount = iNumberOfDocs
    End Function</pre>

    Per VBA help, I've just played around with putting this function into a class module - thought that would work as long as there is an instance of the class open - but on first try it doesn't.

    Way past bedtime so will have to play some more tomorrow!

    Gary

    P.S. Aren't you supposed to be watching the World Cup right now? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Check whether if a template is open(active) or not (Word 2000)

    I just tried it, and <font color=6495ed>(after I had remembered that the Document_Open() etc all had to go into the ThisDocument object of the template, and not just a randomly created module)</font color=6495ed> it worked perfectly.

    I have had issues in the past with Static variables getting reset. If you have the Visual Basic Editor open and you clock the Stop button this seems to clear them all. It is best to test this sort of thing with the Template and the VB Editor closed.

    StuartR

    PS. I don't do football - but that discussion is probably best left to Scuttlebut

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

    Re: Check whether if a template is open(active) or not (Word 2000)

    Hi again,

    I've just tested it again, this time with the VB Editor closed, and yes it works perfectly. <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    Open the VB Editor and test it again, and it doesn't work!

    Why it shouldn't work with the VB Editor open is a great question....

    Gary

Posting Permissions

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