Link to home
Start Free TrialLog in
Avatar of mvp1985
mvp1985

asked on

How to format autonum numbers to add several 0 before depending if they are one, two or three diggits.

I am looking for a way to format auto numbers, so that they are always the same # of digits. Here is why: the first part of the number is supposed to be the date in a certain format, which works great, and the second part should be an auto-number which also works ok, but not how I need it. So how can I make this number: 201411261 look like this: 20141126001 as if it would always be a three digit number, (there is no case that the autonumber would ever be more than 3 digits)?
Thanks for your help
Avatar of Eirman
Eirman
Flag of Ireland image

An imperfect possible workaround
.... start the auto numbering from 1001
When the document is complete, you can run a VBA macro to replace the Autonum fields with formatted numbers. This code has been tested in Word 2007, Windows 7.
Sub FormatAutoNum()
    Dim fld As Field
    Dim n As Integer
    Dim rng As Range
    Dim f As Integer
    
    For f = ActiveDocument.Fields.Count To 1 Step -1
        Set fld = ActiveDocument.Fields(f)
        If fld.Type = wdFieldAutoNum Then
            Set rng = fld.Code
            fld.Unlink
            rng.Expand wdWord
            n = Val(rng.Text)
            rng.Text = Format(n, "000")
        End If
    Next f

End Sub

Open in new window

Avatar of mvp1985
mvp1985

ASKER

How can I start autonumbering at 1001 then?
If your version of Word supports it, you could use ListNum fields with the \s switch in the first one.
{LISTNUM \s 1000}
Avatar of mvp1985

ASKER

I just tried ListNum, but it does not add an additional number like AutoNum.
It isn't clear exactly what the difference is. The number seems to increment as expected.User generated image
I messed that up. Try again:

Image of codes:User generated image
Image of results:User generated image
ASKER CERTIFIED SOLUTION
Avatar of Eric Fletcher
Eric Fletcher
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial