An AutoNew macro can be used to assign an up seqeuncing number to each new document based on the template. You will first need to set the cursor where you want the number to appear and run the "InvoicerSetup" procedure:
Code:
Option Explicit
Dim oBMs As Bookmarks
Dim lngCount As Long
Dim strNumber As String
Dim strLeadingText As String
Dim strTrailingText As String
Sub AutoNew()
AddSeqNumFromRegistry
lbl_Exit:
Exit Sub
End Sub
Sub InvoicerSetup()
Set oBMs = ActiveDocument.Bookmarks
'Setup the bookmark
If MsgBox("Define/redefine the SeqNum" _
& " bookmark at the current insertion point.", _
vbYesNo, "Define bookmark") = vbYes Then
'If already exists then delete it
If oBMs.Exists("SeqNum") Then
oBMs("SeqNum").Range.Delete
End If
'Define and setup empty bookmark
Selection.Collapse wdCollapseStart
Selection.Bookmarks.Add "SeqNum", Selection.Range
Else
'Force setup of bookmark
If Not oBMs.Exists("SeqNum") Then
MsgBox "Place the insertion point where you want the" _
& " SeqNum bookmark and start this procedure again."
Exit Sub
End If
End If
'Seup/Reset starting sequence number.
strNumber = GetSetting("Simple Invoice", "Settings", "Number")
strNumber = InputBox("Set/reset startinbg sequence number? The" _
& " current next number is: " & Val(strNumber), _
"Sequence Number", Val(strNumber))
SaveSetting "Simple Invoice", "Settings", "Number", strNumber
'Setup leading text
strLeadingText = InputBox("Add leading text?", "Leading Text", "Invoice No: ")
SaveSetting "Simple Invoice", "Settings", "Leading Text", strLeadingText
strTrailingText = InputBox("Add trailing text?", "Trailing Text")
SaveSetting "Simple Invoice", "Settings", "Trailing Text", strTrailingText
'Do the deed
AddSeqNumFromRegistry
lbl_Exit:
Exit Sub
End Sub
Sub AddSeqNumFromRegistry()
Dim BMRange As Range
Set oBMs = ActiveDocument.Bookmarks
lngCount = Val(GetSetting("Simple Invoice", "Settings", "Number"))
strLeadingText = GetSetting("Simple Invoice", "Settings", "Leading Text")
strTrailingText = GetSetting("Simple Invoice", "Settings", "Trailing Text")
strNumber = Format(lngCount, "000")
If strTrailingText <> "" Then
strTrailingText = " " & strTrailingText
End If
'Identify current Bookmark range and insert text
On Error GoTo Handler:
Set BMRange = oBMs("SeqNum").Range
BMRange.Text = strLeadingText & " " & strNumber & strTrailingText
'Re-insert the bookmark
oBMs.Add "SeqNum", BMRange
ActiveDocument.Fields.Update
lngCount = lngCount + 1
strNumber = CStr(lngCount)
SaveSetting "Simple Invoice", "Settings", "Number", strNumber
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "The SeqNum bookmark has been deleted. You must run" _
& " the InvoicerSetup routine to redine the bookmark?", _
vbOKOnly, "Missing bookmark!"
Exit Sub
End If
End Sub