In Word VBA, after Range.InsertParagraphBefore, how to set the style on ONLY the inserted paragraph

When I run this code, I get almost everything I want, except that both the inserted paragraph and the one after it are set to style=Heading 3.  I only want the inserted paragraph set to Heading 3.  I can see why it happens --- InsertParagraphBefore extends the range rather than resetting it, which is not what I want.

Sub InsertIndexHeadings()
' INSERT ALPHA HEADINGS IN INDEX
' Go through every paragraph with style=index1 and if its first letter is different from the first letter of the
' previous paragraph, insert a new paragraph with n-dash letter n-dash and set it to style Heading 3.

    Dim para As Paragraph
    Dim sty As String
    Dim first As String
    Dim current As String
    Dim rng As Range
   
    current = ""
   
    For Each para In ActiveDocument.Paragraphs
        sty = para.Style
        If sty = "index1" Then
            first = UCase(Left(para, 1))
            If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", first) > 0 Then
                If current <> first Then
                    Set rng = ActiveDocument.Range(start:=para.Range.start, End:=para.Range.End)
                    With rng
                        .InsertParagraphBefore
                        .InsertBefore Chr(150) & first & Chr(150)
                        .Style = "Heading 3"   ' This does not work as desired
                    End With
                    current = first
                End If
            End If
        End If
    Next para
End Sub


 
Answer
Answer
Try:

Sub InsertIndexHeadings()
 ' INSERT ALPHA HEADINGS IN INDEX
 ' Go through every paragraph with style=index1 and if its first letter is different from the first letter of the
 ' previous paragraph, insert a new paragraph with n-dash letter n-dash and set it to style Heading 3.
 
    Dim para As Paragraph
     Dim sty As String
     Dim first As String
     Dim current As String
     Dim rng As Range
   
     current = ""
    
    For Each para In ActiveDocument.Paragraphs
         sty = para.Style
         If sty = "index1" Then
             first = UCase(Left(para, 1))
             If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", first) > 0 Then
                 If current <> first Then
                     Set rng = ActiveDocument.Range(Start:=para.Range.Start, End:=para.Range.End)
                     With rng
                         .InsertParagraphBefore
                         .InsertBefore Chr(150) & first & Chr(150)
                     End With
                     Set rng = rng.Paragraphs(1).Range
                     With rng
                         .Style = "Heading 3"   ' This does not work as desired
                     End With
                     current = first
                 End If
             End If
         End If
     Next para
 End Sub
 
Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

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 October 5, 2021 Views 1,342 Applies to: