We help IT Professionals succeed at work.

creating an autonumber in a word 2007 document

arhame
arhame asked
on
Hello,

We've create a single page gift certificate that we are giving away to select individuals.  What I'm trying to do is to create some type of autonumbering field so that each certificate has a different certificate number when printed, i.e. certifiate 1001, certificate 1002, certificate 1003, etc, etc.....

Would anyone know how I might go about doing this?

I've played with using page number, but then when printing I realized that I'm still always printing page one.  I'm at a loss.  Any ideas?

Thanks a million.
Comment
Watch Question

Commented:
The easiest way i think would be to create a single file that holds all of the certificates.  Within that file you can start the autonumber-ing with any value (not just 1) by following these instructions: http://www.wordbanter.com/showthread.php?p=419123

from there, each new page will be a new certificate incremented by one.
Top Expert 2011

Commented:
Are all the certificates in teh one document, (i.e. try a paragraph number starting at 1001 - say level 9 which would not affect normal flow).

If at different times then we need to save the datum, different possibilities exist but will address thast if necessary.

CHris
GrahamSkanRetired
Top Expert 2012

Commented:
Try this macro in the ThisDocument module of the certificate document's template. Note that it needs a bookmake in the template to say where the number is to go
Private Sub Document_New()
    Dim iCertNo As Integer
    
    iCertNo = GetSetting("Gifts", "Certificates", "Next Number", 1001)
    ActiveDocument.Bookmarks("CertNumber").Range.Text = iCertNo
    SaveSetting "Gifts", "Certificates", "Next Number", iCertNo + 1
End Sub

Open in new window

GrahamSkanRetired
Top Expert 2012

Commented:
Well it actually needs a bookmark, not a 'bookmake'

Author

Commented:
Thanks for the response.  Due to my lack of scrripting/coding experience, I need to ask.

How do I access the ThisDocument module of this particular document and how would I go about creating that bookmark?

Thanks again.
GrahamSkanRetired
Top Expert 2012

Commented:
Open the VBA editor with Alt+F11. Find your document or template in the left-hand panel. Expand it with the '+' button, and do the same for the Microsoft Word Objects that is revealed. You should the see the ThisDocument module. Double-click to open it and paste the code into the code panel on the right.

If the document is not yet a template, save it as a macro-enabled template (.dotm extension). Put it next to the Normal template. You can see where that is via: Office button, 'Word Options...' button, 'Advanced', 'File Locations...', 'User Templates' entry.

Then you will see it as a choice when you go to create a new document.

Author

Commented:
Thanks a million and that part worked.

How might I go about creating the bookmark you mentioned that allows me to place the automatic numbering within the document?

Sorry to have to ask so many questions.
GrahamSkanRetired
Top Expert 2012

Commented:
Select the place on the document where you need the number. Choose 'Bookmark' from the' Links' group on the ribbon's 'Insert' tab, and type the bookmark name into the box on the dialogue.
GrahamSkanRetired
Top Expert 2012

Commented:
At some stage you may want to reset the numbering back to the start. This is the code to do that:

SaveSetting "Gifts", "Certificates", "Next Number", 1001

Author

Commented:
Hrm.  I've inserted the bookmark as you suggested, and still no numbering when I print.

There is a location on the document where it reads:

CERTIFICATE #

Behind the # sign, I inserted the bookmark (although, I can't see anything there after doing so).  I've tried even keying in a number, selecting it, and then inserting the bookmark. (thinking that maybe the number I keyed in would increment).  Nothing.

What might I be missing?
GrahamSkanRetired
Top Expert 2012

Commented:
There is an option to show the position of bookmarks in the document. It's one of the Advanced set, under 'Show document content'.

For the macro to run automatically, you have to create a new document from the template. If you want to test it beforehand, put the cursor somewhere in the macro and press F5.

Author

Commented:
Ok, I did as you stated, and the certificate number shows up now.  However, when I go to print it, it only prints with Certificate #1001.  For example, I created the new document using that template, and choose to print 3 copies, each copy only has Certificate #1001.  It is not incrementing as I need it to.

Any idea?
GrahamSkanRetired
Top Expert 2012

Commented:
Sorry. I understood that you were creating a number of separate certificates, each one to have its own number.

When you use the 'copies' parameter in the Print dialogue, each copy is identical. This is by design. The document is sent to the spooling mechanism only once, along with the Copies parameter.

Author

Commented:
How can I achieve what I am trying to do, i.e. print a number of certificates with different incremental numbers?
GrahamSkanRetired
Top Expert 2012

Commented:
It is hard to make it rigorous, but I will see what can be done.
GrahamSkanRetired
Top Expert 2012

Commented:
Here are two macros, instead of the first one.

To test, place it in a code module, and run PrintCerts.

Sub PrintCerts()
Dim strInput As String
Dim iCount As Integer
Dim iStart As Integer
Dim iCertNo As Integer
Dim bValid As Boolean

bValid = False

Do
    strInput = InputBox("Please enter number to print", "Certificate Printing", 1)
    If strInput = "" Then
        Exit Sub
    End If
    If IsNumeric(strInput) Then
        If strInput > 0 Then
            If Int(strInput) = strInput Then
                bValid = True
            End If
        End If
    End If
    If Not bValid Then
        MsgBox "Please enter an integer greater than zero"
    End If
Loop Until bValid

bValid = False

strInput = GetSetting("Gifts", "Certificates", "Next Number", 1001)
Do
    strInput = InputBox("Please enter starting number", "Certificate Printing", strInput)
    If strInput = "" Then
        Exit Sub
    End If
    If IsNumeric(strInput) Then
        If strInput > 0 Then
            If Int(strInput) = strInput Then
                bValid = True
            End If
        End If
    End If
    If Not bValid Then
        MsgBox "Please enter an integer greater than zero"
    End If
Loop Until bValid

iStart = strInput
For iCertNo = iStart To iStart + iCount
    FillBookmarkText ActiveDocument, "CertNumber", CStr(iCertNo)
    ActiveDocument.PrintOut
    SaveSetting "Gifts", "Certificates", "Next Number", iCertNo + 1
Next iCertNo
End Sub

Sub FillBookmarkText(objDoc As Word.Document, strBookmarkName As String, strText As String)
    Dim objRange As Word.Range
    Set objRange = objDoc.Bookmarks(strBookmarkName).Range
    objRange.Text = strText
    objDoc.Bookmarks.Add strBookmarkName, objRange 're-add the bookmark so that it *contains* the new text
End Sub

Open in new window

Author

Commented:
Ok, I opened the document, Pressed F11 to open the visual basic editor, copied and pasted the code you offered, and ran it.

It prompted me for the number of copies and then the starting number.

After that, A Visual Basic error popped up that reads:
"The requested member of the collection does not exist"
GrahamSkanRetired
Top Expert 2012

Commented:
It sounds as if the bookmark (CertNumber) isn't in the document.

Author

Commented:
woops.

Ok, I added the bookmark, and the first one prints when I enter 5 as the number.

It also starts with the correct certificate number.

However, it only prints the first certificate, with the correct number, but that is it.  It stops after the first page.
Retired
Top Expert 2012
Commented:
Some last minute editing meant that I had left out a vital code line.

I have turned the two very similar blocks of code into a function, so that there are now three procedures:

Sub PrintCerts()

    Dim iCount As Integer
    Dim iStart As Integer
    Dim iDefaultStart As Integer
    Dim iCertNo As Integer
    
    iCount = GetInputInteger("Please enter number to print", "Certificate Printing", 1)
    If iCount > 0 Then
        iDefaultStart = GetSetting("Gifts", "Certificates", "Next Number", 1001)
        iStart = GetInputInteger("Please enter starting number", "Certificate Printing", iDefaultStart)
        If iStart > 0 Then
            For iCertNo = iStart To iStart + iCount - 1
                FillBookmarkText ActiveDocument, "CertNumber", CStr(iCertNo)
                'MsgBox "Printing " & iCertNo
                ActiveDocument.PrintOut
                SaveSetting "Gifts", "Certificates", "Next Number", iCertNo + 1
            Next iCertNo
        End If
    End If
End Sub

Function GetInputInteger(strPrompt As String, strTitle As String, iDefault As Integer) As Integer
    Dim strInput As String
    Dim bValid As Boolean
    
    strInput = InputBox(strPrompt, strTitle, iDefault)
    
    Do
        If strInput = "" Then
            Exit Function
        End If
        If IsNumeric(strInput) Then 'is the input a number?
            If strInput > 0 Then 'is it a positive number?
                If Int(strInput) = strInput Then  'is it an integer?
                    bValid = True
                End If
            End If
        End If
        If Not bValid Then
            MsgBox "Please enter an integer greater than zero"
            strInput = InputBox(strPrompt, strTitle, strInput)
        End If
    Loop Until bValid
    GetInputInteger = strInput
End Function

Sub FillBookmarkText(objDoc As Word.Document, strBookmarkName As String, strText As String)
    Dim objRange As Word.Range
    Set objRange = objDoc.Bookmarks(strBookmarkName).Range
    objRange.Text = strText
    objDoc.Bookmarks.Add strBookmarkName, objRange 're-add the bookmark so that it *contains* the new text
End Sub

Open in new window

Author

Commented:
Works Perfect.

A million thanks.

Author

Commented:
Thank you for the help