Convert Manual Numbering 1.0, 1.1, 1.1.1, 1.1.2 to automatic numbering - Word 2007 VBA Macro

Link to the Example File.

I saw a question Doug answered in 2012 and I tried to impliment it but it doesn't seem to be working out for me. I was hoping the community could shed some light in the situation. I'm new to VBA and am trying my darndest but I must acquiesce to my ignorance.

Here's the question I'm refering to:

I'm doing a mass conversion of word documents from one company's layout to another companies layout. During this process I want to keep intigrate the files manually typed out numbering system to the new files automatic numbering system so it makes any revisions in the future easier.

Some numbering exists in the old format so I do the following:
ActiveDocument.Range.ListFormat.ConvertNumbersToText

I then want to convert the text numbers (as seen in the attached doc) to an automatic numbering system in the same format. It'd be nice if it put them in their respective levels.

Example:
(manually typed text)
1.0

1.1

1.1.1

1.1.2

1.1.2.1

to

(automatic list formatting)

1.0

     1.1

          1.1.1

          1.1.2

               1.1.2.1

Do you know if this is possible?

Thanks everyone!

Ryan

**Edit: added missing code

Answer
Answer

You will need to have appropriately defined Heading styles in the template and then the following should work

Selection.HomeKey wdStory
 With Selection.Find
     Do While .Execute(FindText:="[0-9]{1,}.[0-9]{1,}.[0-9]{1,}.[1-9]{1,}^t[A-z]{1,}", Forward:=True, _
     MatchWildcards:=True, Wrap:=wdFindContinue) = True
         With Selection
             .Paragraphs(1).Style = wdStyleHeading4
             .text = Mid(.text, InStr(.text, vbTab) + 1)
         End With
     Loop
 End With
 With Selection.Find
     Do While .Execute(FindText:="[0-9]{1,}.[0-9]{1,}.[1-9]{1,}^t[A-z]{1,}", Forward:=True, _
     MatchWildcards:=True, Wrap:=wdFindContinue) = True
         With Selection
             .Paragraphs(1).Style = wdStyleHeading3
             .text = Mid(.text, InStr(.text, vbTab) + 1)
         End With
     Loop
 End With
 With Selection.Find
     Do While .Execute(FindText:="[0-9]{1,}.[1-9]{1,}^t[A-z]{1,}", Forward:=True, _
     MatchWildcards:=True, Wrap:=wdFindContinue) = True
         With Selection
             .Paragraphs(1).Style = wdStyleHeading2
             .text = Mid(.text, InStr(.text, vbTab) + 1)
         End With
     Loop
 End With
 With Selection.Find
     Do While .Execute(FindText:="[0-9]{1,}.0^t", Forward:=True, _
     MatchWildcards:=True, Wrap:=wdFindContinue) = True
         With Selection
             .Paragraphs(1).Style = wdStyleHeading1
             .text = Mid(.text, InStr(.text, vbTab) + 1)
         End With
     Loop
 End With

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

Try:

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 = vbNullString
          Para.Style = "Heading " & iLvl
        End If
      End If
    End With
  Next
End With
End Sub

Note: the above macro assumes the headings are linked to the numbering levels. If your heading Styles lack the auto multi-level list numbering, you can re-apply it via the following macro. As coded, the headings are also indented (in 0.5cm increments) according to their level.

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)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = CentimetersToPoints(0.5 + i * 0.5)
    .ResetOnHigher = True
    .StartAt = 1
    .LinkedStyle = "Heading " & i
  End With
Next
End Sub

Cheers
Paul Edstein
(Fmr MS MVP - Word)

3 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated June 7, 2024 Views 9,889 Applies to: