How to count the number of times when word document is opened?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Riley
    New Member
    • Jan 2011
    • 4

    How to count the number of times when word document is opened?

    What is a simple VBA code to count the number of times a word document is opened. And how do you post this number in your document.

    Some clear code as I can get this to work using excel
    but I can't seem to get the code right in word 2007.

    A few lines of clear code for a newbe like myself would be greatly appreciated.

    This is what I have. I can't get it to work so far.
    Code:
    Sub AutoOpen()
    
    Order = System.PrivateProfileString("C:\Settings.Txt", _
          "MacroSettings", "Order")
    
    If Order = "" Then
        Order = 1
    Else
        Order = Order + 1
    End If
    
    System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
          "Order") = Order
    
    End Sub
    Last edited by Niheel; Jan 25 '11, 05:07 PM. Reason: added code to question
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Post your word code and we can take a look.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Can you post what's in your C:\Settings.txt ?

      Comment

      • Stephen Riley
        New Member
        • Jan 2011
        • 4

        #4
        MacroSettings]
        Order=6
        Looks like it does work. I just need to know how to get
        this number to show up in the document.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Well, how do you get the number in Excel? You can do the same thing in Word, probably in the footer.

          Comment

          • Stephen Riley
            New Member
            • Jan 2011
            • 4

            #6
            In excel I tell it to go to a particular cell, e.g. A1

            So in the footer how would that look.

            Sorry to be dense I'm still learning.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              I don't know but a google search gave me this
              Code:
                 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
                 Selection.HeaderFooter.Range.Text = "MyText"

              Comment

              • Stephen Riley
                New Member
                • Jan 2011
                • 4

                #8
                Dam it worked!

                Thank you for the help!!

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  Stephen,

                  Please flag Rabbit as having given you the correct answer, so the rest of us can learn from both the question and answer. If it is clear what the answer to the question is, then folks who read the thread in the future will be helped, rather than assume that nothing was resolved.

                  Cheers!
                  Oralloy

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    BTW, another option is to put the value in a document attribute, and then use a field to place that attribute in the body of the document.

                    Comment

                    Working...