• temporary variables but not document variables (WORD 97/2000/…)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » temporary variables but not document variables (WORD 97/2000/…)

    Author
    Topic
    #416559

    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

    Viewing 3 reply threads
    Author
    Replies
    • #932210

      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.

    • #932212

      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 …

    • #932225

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

      Alan

      • #932233

        Alan,

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

        Robie

        • #932269

          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

        • #932353

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

          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…

        • #932416

          Here’s a possible solution, along the lines of what Jefferson proposed. It seems to work in my test scenario:
          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

          Alan

          • #933056

            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

    • #932429

      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.

    Viewing 3 reply threads
    Reply To: temporary variables but not document variables (WORD 97/2000/…)

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

    Your information: