Inserting a counter in a Word Document

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been attempting to insert a counter in a Word document which would increment each time the document is openned
Can anyone help?
 
Do you want this to be a visible counter that appears somewhere inside the
document? If so, one way (there are many) would be using a bookmarked number
and an AutoOpen macro.

Type the starting number (e.g., 1) anywhere in the document, select the
number, and insert a bookmark named Counter. With Bookmarks set to view,
gray brackets will appear around the number (Tools - Options - View -
Bookmarks).

Create the following macro, naming it AutoOpen, and saving it in the
document itself:

Sub AutoOpen()
Selection.GoTo What:=wdGoToBookmark, Name:="counter"
Selection.Copy
NewValue = Selection + 1
Selection.TypeText (Str(NewValue))
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Counter"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub

Save/close the document.

Now (depending on your macro security settings), each time you open that
document, the number will be incremented by 1.

Above, I said "depending on your macro security settings." Here, I have
security set extremely high, so to get the macro to work, I had to digitally
sign it, and tell Word to always trust macros signed by me. Office comes
with a SelfCert program that creates a digital signature for use with your
own macros. For additional help, you might try one of the vba newsgroups
(e.g., microsoft.public.word.vba.general).

--
Herb Tyson MS MVP
Please respond in the newsgroups so everyone can follow along.
http://www.herbtyson.com
olleber said:
I have been attempting to insert a counter in a Word document which would
increment each time the document is openned.
 
Thank you very much, Herb. Your instructions were superb. it is doing exactly what I wanted it to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top