View Single Post
 
Old 01-14-2022, 04:11 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Dec 2020
Posts: 170
Shelley Lou is on a distinguished road
Default VBA Help Indent Paragraphs between Headings

Test Doc to replace non numbered paras under numbered paras.docx

Hi, I'm hoping someone can help me with a macro I am trying to create that indents non-numbered paragraphs under Heading 1-7 but ignores if the next paragraph is a heading number, so:

Heading 1 = Body1
Heading 2 = Body2
Heading 3 = Body3
Heading 4 = Body4
Heading 5 = Body5
Heading 6 = Body6
Heading 7 = Body7

So far the code works for Heading 1 but is removing the heading 2 numbering and making those as Body1. I haven't got as far as adding in the other heading/body levels until I can sort out this problem first.

If anyone can advise what I'm missing in the code I would be really grateful.
Thanks, Shelley


Code:
Sub ChangeParasAfterHeading()
    Dim para As Paragraph, nextPara As Paragraph, oRng As Range
    If Selection.Type = wdSelectionIP Then
 MsgBox Prompt:="You have not selected any text!"
 Exit Sub
End If
    Set oRng = Selection.Range
    With oRng
    For Each para In oRng.Paragraphs
        If para.Style = "Heading 1" Then
            Set nextPara = para.Next
            If Not nextPara Is Nothing Then
                nextPara.Style = "Body1"
    Else
    If para.Style = "Heading 2" Then
            Set nextPara = para.Next
            If Not nextPara Is Nothing Then
                nextPara.Style = "Body2"
                Else
            End If
        End If
        End If
        End If
    Next
End With
End Sub
Reply With Quote