Deleting Empty Paragraphs in Word - but not objects/pictures

Viewed 37

I'm using the macro from @Wayne G. Dunn on this question and it works great, but it doesn't understand when there are pictures/objects in my document and deletes them.

Would there be any way around it?

The said document is a Word file exported from an online app/software we use at work, if it helps. I don't know the specs of the picture.

1 Answers

The code in your linked thread is awful and is indeed liable to delete shapes attached to paragraphs. There is also no need to loop through every paragraph. Indeed, a macro isn't even needed for most cases. All you need is a wildcard Find/Replace, where:

Find = [^13]{2,}
Replace = ^p

As a macro, this becomes:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[^13]{2,}"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

Note: For systems using non-english regional settings, you may need to use:

[^13]{2;}

instead of:

[^13]{2,}

in both the Find/Replace and the macro.

Related