Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Run AutoOpen only once (2003/SP3)

    Is there a way to use VBA code so that the AutoOpen macro executes only once? I have a situation where I need my users to fill out a userform opened by my AutoOpen macro, but once they are finished and they save and re-open the document, I don't want them to see the userform again. The reason I am forced into this situation is because I am prevented from using templates, and the users will be opening read-only versions of Word documents from SharePoint.

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

    Re: Run AutoOpen only once (2003/SP3)

    You could use a document variable to keep track of whether AutoOpen has already run:
    <code>
    Sub AutoOpen()
    Dim f As Boolean
    On Error GoTo ErrHandler
    f = ActiveDocument.Variables("HasRun")
    If f = True Then
    Exit Sub
    End If

    ContinueHere:
    ActiveDocument.Variables("HasRun") = True
    ' Code to execute first time goes here
    ...
    Exit Sub

    ErrHandler:
    ActiveDocument.Variables.Add "HasRun", True
    Resume ContinueHere
    End Sub
    </code>
    The code checks the value of the HasRun document variable.
    If the variable exists and is True (i.e. if the code has run before), the code exits.
    If the variable doesn't exist, it is created.
    The value of the variable is set to True, whether it already existed or not, then the rest of the code is executed.

    If you have experimented with the code, you can reset the document by executing
    <code>
    ActiveDocument.Variables("HasRun") = False
    </code>
    from the Immediate window.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Run AutoOpen only once (2003/SP3)

    Hans,
    Gee, Hans, that actually worked! Thanks a lot! I was a little confused at first by the 'Code to execute first time goes here' until I realized I was supposed to put the code to open my userform there. With your code, what I have is a simulation of SharePoint handling a Word template. Once the user is through completing the userform, saves, re-opens, and doesn't see the userform again, it will appear as if the document he created was based on a template, when actually it is only a copy of a .doc but with a neutered AutoOpen. (I am forced to deny the users checkout privileges to prevent them from overwriting the "template" upon check in, so I have them open a read-only copy.)

Posting Permissions

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