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
Microsoft Word

Avatar of undefined
Last Comment
Eric Fletcher

8/22/2022 - Mon
Eirman

An imperfect possible workaround
.... start the auto numbering from 1001
GrahamSkan

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

mvp1985

ASKER
How can I start autonumbering at 1001 then?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
GrahamSkan

If your version of Word supports it, you could use ListNum fields with the \s switch in the first one.
{LISTNUM \s 1000}
mvp1985

ASKER
I just tried ListNum, but it does not add an additional number like AutoNum.
GrahamSkan

It isn't clear exactly what the difference is. The number seems to increment as expected.Image of field results
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
GrahamSkan

I messed that up. Try again:

Image of codes:Field codes
Image of results:ListNumResults.PNG
ASKER CERTIFIED SOLUTION
Eric Fletcher

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question