Serial Number Macro Problem with zeroes

C

Code Numpty

I use the following macro in Word 2003 to produce sequentially numbered
certificates & have 2 problems with it.


Sub SEQserialnumber()
'
' SEQserialnumber Macro
' Macro created 11/09/2007 by Sharon
'
Dim Counter As Long
Dim Message As String, Title As String, Default As String, NumCopies As
Long
Dim Rng1 As Range

' Set prompt.
Message = "Enter the number of copies that you want to print. Get
it right you can't stop it!"
' Set title.
Title = "Print"
' Set default.
Default = "1"

' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
' Edit from Jay Freedman to enable user to specify serial number
Message = "Enter the starting serial number"
Title = "Serial Number"
SerialNumber = Val(InputBox(Message, Title, Default))

'Next line added to cause Ask field to fire pop-ups
ActiveDocument.Fields.Update

Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0

While Counter < NumCopies
Rng1.Delete
Rng1.Text = SerialNumber
ActiveDocument.PrintOut
SerialNumber = SerialNumber + 1
Counter = Counter + 1
Wend

'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
.Add Name:="SerialNumber", Range:=Rng1
End With
'Next line added to update { REF SerialNumber }
'This is more selective than just updating all the fields; in
'particular, the Ask fields (which have .Type = wdFieldAsk) won't be
'updated the second time.
Dim Fld As Field
For Each Fld In ActiveDocument.Fields
If Fld.Type = wdFieldRef Then Fld.Update
Next

ActiveDocument.Close SaveChanges:=False

End Sub

Problem 1: I know find out that serial numbers begin with zeroes e.g.
0000001 to 0000005. If zeroes are entered they do not print and they must do
so to much pre-printed labels.

Problem 2: The 2nd serial number instance is not printing. This is a
mergefield in the document as below.
{ REF SerialNumber \*MERGEFORMAT }

I would be truly grateful for some help with both of these, thanks.
 
G

Greg Maxey

Sub SEQserialnumber()
Dim Counter As Long
Dim NumCopies As Long
Dim Rng1 As Range
Dim SerialNumber As String
Dim Fld As Field

NumCopies = Val(InputBox("Enter the number of copies that you want to
print." _
& vbCr + vbCr & "Getit right you can't stop it!", "Print", "1"))

SerialNumber = Val(InputBox("Enter the starting serial number", "Serial
Number", _
"0000001"))

ActiveDocument.Fields.Update

Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0

While Counter < NumCopies
Rng1.Delete
Rng1.Text = Format(SerialNumber, "000000#")
With ActiveDocument.Bookmarks
.Add Name:="SerialNumber", Range:=Rng1
End With
For Each Fld In ActiveDocument.Fields
If Fld.Type = wdFieldRef Then Fld.Update
Next
ActiveDocument.PrintOut
SerialNumber = SerialNumber + 1
Counter = Counter + 1
Wend
ActiveDocument.Close SaveChanges:=False
End Sub
 
C

Code Numpty

Thanks Greg but these 2 lines of code are showing a compile error

NumCopies = Val(InputBox("Enter the number of copies that you want to
print." _
& vbCr + vbCr & "Getit right you can't stop it!", "Print", "1"))

SerialNumber = Val(InputBox("Enter the starting serial number", "Serial
Number", _
"0000001"))
 
C

Code Numpty

Sorry, it was a line break problem. Now works a treat. I just wish I knew why.
Thanks :)
 
D

David Sisson

I use the following macro in Word 2003 to produce sequentially numbered
certificates & have 2 problems with it.
SerialNumber = Val(InputBox(Message, Title, Default))

Your treating the response as a number (integer/long - You don't have
it defined.)

Define it as a string.

Dim SerialNumber as String

Then

SerialNumber = InputBox(Message, Title, Default)

If you need to strip the leading zeros later, you can use the Val().

On other note, you can limit the number of copies.

' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
If NumCopies > 10 then NumCopies = 10.

Limits the total number of copies to ten to prevent a 100 copies due
to input mistake.
 

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