Sequential Number

R

Ravi Sandhu

Hi guys,



I want a particular Word template to have a sequential number each time it
is opened.



How can this be done please?
 
S

Steve Yandl

Here is an alternate approach that has worked for me.

Create this subroutine and run it once in a new docoment:

Sub CreateCounter()
Dim aVar As Variable
Dim N As Integer
For Each aVar In ActiveDocument.Variables
If aVar.Name = "myCounter" Then N = aVar.Index
Next aVar
If N = 0 Then
ActiveDocument.Variables.Add Name:="myCounter", Value:=Format$(0, "0000")
Else
ActiveDocument.Variables(N) = Format$(0, "0000")
End If
End Sub

After running the subroutine, go back to the main document and insert a
field of type "DocVariable" where the count is supposed to appear and name
it "myCounter". Alt plus F11 and under "ThisDocument" create the following
subroutine:

Private Sub Document_New()
Dim I As Integer
I = CInt(ThisDocument.Variables("myCounter").Value)
I = I + 1
ThisDocument.Variables("myCounter").Value = I
ThisDocument.Save
ActiveDocument.Variables("myCounter").Value = Format$(I, "0000")
ActiveDocument.Fields.Update
End Sub

Go back to the main document and after finsising up anything beside the
count that should be part of a template, save the file as a document
template, "dot". Each time a new document is created from this template,
the document variable is incremented by one and the change is saved to the
template.

Steve
 
S

Steve Yandl

Should have included in the earlier post that if the template might be
shared, there should be a way to reset it's stored counter variable. The
following subroutine can be placed in the template to do that:

Sub ManuallySetCounter()
Dim C As Integer
C = CInt(ThisDocument.Variables("myCounter").Value)
C = CInt(InputBox("Change Counter Value", "Reset", C))
ThisDocument.Variables("myCounter").Value = C
ThisDocument.Save
ActiveDocument.Variables("myCounter").Value = Format$(C, "0000")
ActiveDocument.Fields.Update
End Sub


Steve
 

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

Top