Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    temporary variables but not document variables (WORD 97/2000/...)

    Hi

    I suspect the answer is probably no to this one but I will ask the question anyway:

    Is there a way of defining/declaring a variable that is specific to an instance of a document - i.e. it is created/defined when the document is created/opened and goes away when the document closes - it doesn't matter if the document is saved or not.

    The problem manifests itself as follows:I have template that displays a userform when a document is CREATED or OPENED (required by the powers that be). I have created macros AutoNew and AutoOpen to display the form. The problem arises when I open a document, hyperlink to another document, click left arrow/back button (to return to the original document), & lo & behold it runs the AutoOpen macro (which displays the form ). I only want to display the form once - the very first time a document is created/opened.

    I can't think of simple or even a hard way doing this at the moment.

    Any help highly appreciated.

    Thanks in advance.

    Robie
    Thanks.
    Robie

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

    Re: temporary variables but not document variables (WORD 97/2000/...)

    If you want the form to be displayed when the document is initially created, you only need code in the AutoNew macro or in the Document_New event procedure of the template, not in AutoOpen or Document_Open. Since every document must be created before it is opened, that would seem to be sufficient, and as far as I can see, it would take care of your problem.

  3. #3
    5 Star Lounger
    Join Date
    Oct 2002
    Location
    Wellington, Wellington, New Zealand
    Posts
    621
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    I'm struggling with understanding the specific request
    As I read it, the form displays at open - you want it to never display again until you exit / close the document but are happy that it displays again tomorrow?
    Where I'm confused is that you appear to never want it to display again ever (tomorrow, next year ...) by stating "only display the form once"?

    If the first is true, then a VBA variable is perfectly fine
    If the second, I cannot see a way around either document properties, or some clever 'invisible' marker within the file - e.g. a bookmark, a special style, an invisible graphic element ...

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    I think I understand where you're coming from. I tried simulating the situation and find Word's behaviour very strange here. As far as I can tell, there are only ever two documents open in my test situation. The Back arrow on the Web toolbar, which appears in the window containing the second hyperlinked document, simply puts the original (still open) document/ window back into focus. No new document windows are involved. However the Document_Open() event of the first document obviously runs again. <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

    Alan

  5. #5
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    Alan,

    EXACTLY. You have described it 100 times better than I did. Thanks.

    Robie
    Thanks.
    Robie

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    Still no closer to an answer though. It appears that the web arrows (both back and forward) will fire the Document_Open() events (for either document). However, the Document_Close() do NOT seem to fire. I can't fathom how a document can open if it's never been closed, and there's only one copy of it ever visible. ???

    Alan

  7. #7
    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: temporary variables but not document variables

    You can store information in a document in at least two ways, the two I have in mind being document variables and custom document properties. You could set a flag in your AutoOpen procedure, and then clear it in an AutoClose procedure. At least in theory. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Sorry: using AutoClose to reset a document variable or document property would require that the document be saved at closing, which may be undesirable (e.g., the user might want to discard some edits ). So a static VBA variable might work better. If it works...

  8. #8
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    Here's a possible solution, along the lines of what Jefferson proposed. It seems to work in my test scenario:
    <code>Private Sub Document_Open()
    Dim dv As Variable

    For Each dv In ThisDocument.Variables
    If dv.Name = "Opened" Then Exit Sub
    Next dv

    ThisDocument.Variables.Add Name:="Opened", Value:="1"
    // The rest of the usual auto_open stuff follows here

    End Sub

    Private Sub Document_Close()
    On Error Resume Next
    ThisDocument.Variables("Opened").Delete

    // The rest of the usual auto_close stuff follows here
    End Sub
    </code>

    Alan

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,805
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: temporary variables but not document variables

    Robie

    It strikes me that this problem is uniquely caused by returning to the file via a hyperlink. Do you want to see the dialog if the file was actually opened by the hyperlink?

    One possible approach is to compare the most recently opened Doc and the most recently visited hyperlink to clarify how the file was opened. I can help you with the most recently opened doc but haven't yet worked out how to harvest the most recently visited hyperlink in the history list. The following code checks to see if the current doc is the last Word file opened. If it isn't then your dialog should not be called. If it is then you would then need to test whether the current document was the target of the last followed hyperlink.
    Sub AutoOpen()
    If Documents(Documents.Count).Name = ActiveDocument.Name Then
    MsgBox ActiveDocument.Name
    End If
    End Sub

    Another way to kludge a solution might be to query the age of the locking file that indicates when the document was opened. If this was considerably older than the current time then it would be a reasonable assumption that the file was opened previously so the dialog can be avoided.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: temporary variables but not document variables (WORD 97/2000/...)

    Alan,

    As Jefferson pointed out - your solution is good (very good) except it requires the user to save the document when WORD is closed. This would not be the best of action - unfortunately.

    I guess, I will just have to think of some other way of doing it or the powers that be must accept the fact that it will display the form to the user if the user return back from a hyperlinked session.

    Thanks to you and everyone for the effort in finding the solution to this problem. You guys just make our life (not so good with WORD) so much easier. Thanks once again.

    Robie
    Thanks.
    Robie

Posting Permissions

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