• Run AutoOpen only once (2003/SP3)

    Author
    Topic
    #456547

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #1141233

      You could use a document variable to keep track of whether AutoOpen has already run:

      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

      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

      ActiveDocument.Variables("HasRun") = False

      from the Immediate window.

      • #1141267

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

    Viewing 0 reply threads
    Reply To: Run AutoOpen only once (2003/SP3)

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

    Your information: