View Single Post
 
Old 12-11-2020, 01:51 PM
macropod's Avatar
macropod macropod is offline Windows 10 Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The following two macros should do the job for you.

The first applies multi-level list numbering to Word's Heading Styles:
Code:
Sub ApplyMultiLevelHeadingNumbers()
Dim LT As ListTemplate, i As Long
Set LT = ActiveDocument.ListTemplates.Add(OutlineNumbered:=True)
For i = 1 To 9
  With LT.ListLevels(i)
    .NumberFormat = Choose(i, "%1", "%1.%2", "%1.%2.%3", "%1.%2.%3.%4", "%1.%2.%3.%4.%5", "%1.%2.%3.%4.%5.%6", "%1.%2.%3.%4.%5.%6.%7", "%1.%2.%3.%4.%5.%6.%7.%8", "%1.%2.%3.%4.%5.%6.%7.%8.%9")
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = wdListNumberStyleArabic
    .NumberPosition = CentimetersToPoints(-0.5 + i * 0.5)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = CentimetersToPoints(1 + i * 0.5)
    .ResetOnHigher = True
    .StartAt = 1
    .LinkedStyle = "Heading " & i
  End With
Next
End Sub
Headings and their numbers are also indented (in 0.5cm increments) according to their level, though you can change that (e.g. InchesToPoints using instead of CentimetersToPoints would change the indents to 0.5in).

The second macro converts your existing manual numbering to the applicable auto-numbered Heading Styles.
Code:
Sub ApplyHeadingStyles()
Dim Para As Paragraph, Rng As Range, iLvl As Long
With ActiveDocument.Range
  For Each Para In .Paragraphs
    Set Rng = Para.Range.Words.First
    With Rng
      If IsNumeric(.Text) Then
        While .Characters.Last.Next.Text Like "[0-9. " & vbTab & "]"
          .End = .End + 1
        Wend
        iLvl = UBound(Split(.Text, "."))
        If IsNumeric(Split(.Text, ".")(UBound(Split(.Text, ".")))) Then iLvl = iLvl + 1
        If iLvl < 10 Then
          .Text = ""
          Para.Style = "Heading " & iLvl
        End If
      End If
    End With
  Next
End With
End Sub
You may want to change other aspects of the Heading Style formatting to better suit your layout requirements.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote