Word 2010 VBA macro to restart numbered lists

3.6k views Asked by At

I have a Word 2010 document with a lot of lists that were copied from another document. Unfortunately, when I copied the lists into my document they do not start at 1 anymore as they did in the orginial but are continuing from the previous lists leading up to chaos in my document.

I need my macro to run along all the numbered lists in my document and restarts each list at 1. I tried this with listformat.applylisttemplate but when calling my lists with "For Each li In ActiveDocument.Lists" I could not use the listformat function. See my code snippet below.

Set temp3 = wrdApp.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
    With temp3
        .StartAt = 1
End With

For Each li In ActiveDocument.Lists
    li.Range.ListFormat.ApplyListTemplate ListTemplate:=temp3
Next

Obviously I would like something in the form of:

For Each li In ActiveDocument.Lists
    li. => restart list count here
Next

Can anyone help? Thanks!

Addition: my current text looks like below and I'm trying to get the macro to restart each list with 1 in Word:
INPUT:
Sometext
3. TextA
4. TextB
5. TextC
6. TextD

Some other text
21. Text q
22. Text w
23. Text e

OUTPUT after macro:
1. TextA
2. TextB
3. TextC
4. TextD

Some other text
1. Text q
2. Text w
3. Text e

2

There are 2 answers

0
anvilsoup On

Hi I have created a macro here that does exactly what you want.

You need to make the numbered lists as a certain style first. This is to make sure that the macro changes only the lists you need to change.

https://sites.google.com/site/anvilsoup/word/fix-numbered-items

How it works

I've chosen to use a reasonable method of determining when to restart lists: If the previous paragraph isn't a list item, then this paragraph must be a new list!

This logic is what most people use when starting a new list. It's sensible and conventional.

NOTE: If you need to have an unnumbered paragraph nestled within a list, you should consider using line breaks (SHIFT-ENTER) instead of paragraph breaks. Using line breaks will also ensure that indenting is preserved. Don't do strange things like turn off numbering for that paragraph. Technically this text is part of the same numbered item so it should be in the same paragraph.

1
Jeff1265344 On

I don't have an example to try this on but give it a try, perhaps it will work?

Sub ListRestart()
For Each li In ListGalleries(wdNumberGallery).ListTemplates
    li.ListLevels(1).StartAt = 1
Next
End Sub

Edit: I agree, the above doesn't do the trick. The following will identify the correct paragraph where we want to reset the numbering. However, I cannot figure out how to reset the numbering from there. Perhaps that will get someone else closer to the answer?

Sub ListRestart()
' for each paragraph
' If this paragraph is a list (ListType = 3) and last paragraph was not (ListType = 0) then restart numbering
Dim n As Integer, lastListType As Integer
For Each myPara In ActiveDocument.Paragraphs
    n = n + 1
    'Debug.Print n & myPara.Range.Text & " ListType: " & myPara.Range.ListFormat.ListType
    If myPara.Range.ListFormat.ListType = 3 Then
        If lastListType = 0 Then
            Debug.Print n & " " & myPara.Range.Text & " Reset numbering here"
        End If
    End If
    lastListType = myPara.Range.ListFormat.ListType
Next
End Sub