replace two paragraph markers with one

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

replace two paragraph markers with one

Post by Robie »

Hi

I must be doing something stupid here - can't figure out why it is not replacing two paragraph marks with one. It *finds* the two paragraphs if I find one at a time but it *doesn't* replace.

A process creates a Word document where most paragraphs have an extra blank paragraph at the end. Now, I am trying to remove these blank paragraphs but the Find & Replace doesn't work.
62.png
A sample word document is attached.
Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78917
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: replace two paragraph markers with one

Post by HansV »

The second paragraph mark of each pair is different - it has extra properties in the XML code that makes it behave differently. But I can't find what exactly.
Here is a workaround:
- Enter ^p in the 'Find what' box and @@@ in the 'Replace with' box, then click 'Replace All'.
- Only the first paragraph mark in each pair will be replaced.
- Enter @@@ in the 'Find what' box and clear the 'Replace with' box, then click 'Replace All'.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace two paragraph markers with one

Post by Robie »

HansV wrote:The second paragraph mark of each pair is different - it has extra properties in the XML code that makes it behave differently. But I can't find what exactly.
Here is a workaround:
- Enter ^p in the 'Find what' box and @@@ in the 'Replace with' box, then click 'Replace All'.
- Only the first paragraph mark in each pair will be replaced.
- Enter @@@ in the 'Find what' box and clear the 'Replace with' box, then click 'Replace All'.
Thanks Hans, I will try that.
Also, I have just found out that these documents are created using HTML/XML (not sure which one).

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15810
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: replace two paragraph markers with one

Post by ChrisGreaves »

Robie wrote:I must be doing something stupid here -...
Hi Robie, and I see Hans's reply.
You are not stupid (apart from the fact that you are using MSWord, that is :laugh: :clapping: ).

I have had the same problem writing VBA code/macros to remove excess white space since at least Word97.

(I am now at Word2003, and starting to give up hope :rofl: )
Cheers
Chris
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: replace two paragraph markers with one

Post by macropod »

For whatever reason, Word refuses to find & replace a paragraph break that precedes a table. This is a long-standing bug. You could achieve the desired outcome with a macro like:

Code: Select all

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
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    .End = .End - 1
    .Text = vbNullString
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
The 'wdReplaceAll' reduces all consecutive paragraph breaks other than before a table to single paragraph breaks; the loop handles those before tables.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
AlanMiller
BronzeLounger
Posts: 1545
Joined: 26 Jan 2010, 11:36
Location: Melbourne, Australia

Re: replace two paragraph markers with one

Post by AlanMiller »

Just out of curiosity, would it be possible to firstly replace all ^13 instances with ^p, then run the usual search/replace of ^p^p with a single^p? I mean from the UI dialog, rather than from Macropod's macro.

Alan

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: replace two paragraph markers with one

Post by macropod »

Using ^13 makes no difference.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
AlanMiller
BronzeLounger
Posts: 1545
Joined: 26 Jan 2010, 11:36
Location: Melbourne, Australia

Re: replace two paragraph markers with one

Post by AlanMiller »

Yes, I can see the bug when I try it on the sample docx. Most peculiar. Then again, I can also see why there is a need to retain the ^p immediately preceding a table.

Alan