How to remove/add a space after/before all headings with a macro

I have a document with many level 3 headings. I need a space (paragraph break) added before all of them, and a space (paragraph break) removed after each of them. How can I do this?

Thanks in advance.

Answer
Answer

Properly formatted, your document shouldn't contain any double paragraphs. All the spacing should be controlled via the paragraph space before & after options, preferably as part of the corresponding Style definitions. Thus, deleting all double paragraphs and fixing the Style definitions will give you a document with a more consistent layout and whose layout is easily changed to suit any further changes in formatting requirements. That said, you can clean up just the heading 3 paragraphs with a macro like:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[!^13]@^13[^13]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Paragraphs.First.Style = "Heading 3" Then
      i = i + 1
      Do While .Paragraphs.Last.Style <> "Heading 3"
        .Characters.Last.Text = vbNullString
      Loop
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " headings updated."
End Sub

Note: The macro doesn't insert an empty paragraph before the heading - you should insert the desired space before via the heading 3 Style definition.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

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 3,620 Applies to: