View Single Post
 
Old 10-16-2019, 04:20 PM
jeffreybrown jeffreybrown is offline Windows 10 Office 2016
Expert
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default Count number of periods and convert paragraph to a heading style

I found this code which works good to count the number of periods in the numbering and then convert to the appropriate heading style. The numbering is currently not automated.

1.1. dkfsdflk j
1.2. dkfsdflk j
1.2.1. dkfsdflk j

So in the above example, the first two paragraphs will be heading 2 and then the last heading 3. I'm going to have to expand this out to at least heading 6, so instead of writing out the patterns six times, how can we just count the number of periods and apply the style based off of that number?

Code:
Sub Converttoheadingstyle()
   
    Dim oPara   As Paragraph
    Dim wdDoc   As Range
    Dim rgTemp  As Range
    
    If Selection.Type = wdSelectionNormal Then: Set wdDoc = Selection.Range: Else: Set wdDoc = ActiveDocument.Range
   
    For Each oPara In wdDoc.Paragraphs: Set rgTemp = oPara.Range.Duplicate
        
        If rgTemp.Find.Execute(FindText:="[0-9]{1,}.[0-9]{1,}.[0-9]{1,}. ", MatchWildcards:=True) Then
          oPara.Style = ActiveDocument.Styles("Heading 3"): rgTemp.Text = ""
        
        ElseIf rgTemp.Find.Execute(FindText:="[0-9]{1,}.[0-9]{1,}.  ", MatchWildcards:=True) Then
          oPara.Style = ActiveDocument.Styles("Heading 2"): rgTemp.Text = ""
        
        ElseIf rgTemp.Find.Execute(FindText:="[0-9]{1,}.  ", MatchWildcards:=True) Then
          oPara.Style = ActiveDocument.Styles("Heading 1"): rgTemp.Text = ""
        End If
        
      Next oPara
     
End Sub
Reply With Quote