Replace....

A

Al & Cay Grant

Hi Guys,

This is not quite VBA, but I am sure someone here knows the answer.

I have a document which contained unwanted paragraph marks.


Essentially what I want to do is to delete every line that only spaces and
paragraph

marks. Note some lines contain no spaces, just para marks, other lines have
multiple

spaces and then paramarks.



Anyone able to tell me what the find command will be?



Cheers



-Al
 
H

Helmut Weber

Hi,
first thing to is to make shure, that there are no
empty paragraphs at the end of the document, as the
last paragraph mark cannot be replaced. Attempts to
do so result in an andless loop.
So First: PurgeDocEnd
Public Sub PurgeDocEnd()
Dim z As Long
Dim r As Range
With ActiveDocument
z = .Range.End
Set r = .Range(z - 2, z - 1)
While r.Text = " " Or r.Text = Chr$(13)
r.Delete
z = .Range.End
Set r = .Range(z - 2, z - 1)
Wend
End With
End Sub
Then it's easy:
Sub PurgeLines()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = " {2,}^013"
.Replacement.Text = "^013"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With r.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^013{2,}"
.Replacement.Text = "^013"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
Note: .Text = " {2,}^013" is AFAIK the English Version.
Here in Bavaria it's .Text = " {2;}^013"
, versus ; !!
I can't test the English version.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0
 
D

Doug Robbins - Word MVP

Hi Al,

See the article “Finding and replacing characters using wildcards” at:

http://www.mvps.org/word/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jeff Hall

Search for ^p^w^p
and replace with ^p

You might also consider replacing ^l^w^p with ^p

^p = paragraph mark
^w = white space
^l = line break

--------------------------------------------------------------------
Jeff Hall MSc MRICS
Director, Eon Commerce Ltd.
http://www.eon-commerce.com

Software available for you to evaluate before buying...
EasyHTML/Help CHM file Editor for MS Word
http://www.easyhtmlhelp.com
--------------------------------------------------------------------
 
L

Larry

Getting rid of empty spaces between empty paragraph marks is tricky.
Helmut's code does this, but will also delete empty spaces between the
end of the last sentence in each paragraph and the paragraph mark, which
may not be desired. Also, his first search in the PurgeLines macro
should be for .Text = " {1,}^013", not for .Text = " {2,}^013" .

Here is my own macro that I use for this purpose. If you want the macro
to operate on the whole document rather than on the selection or from
the IP downward, simply changes all instances of .Wrap = wdFindStop to
..Wrap = wdFindContinue


Sub TwoParaMarksToOneParaMark()

' Changes instances of two or more consecutive paragraph marks to one.
' Eliminates any spaces between paragraph marks.
' Operates on selection or from IP to end of document.
' If there are empty paragraph marks at end of doc, leaves two empty
paragraph marks.
' Note that replacement text must be ^p not ^13, since ^13 is not a real
para mark.

Application.ScreenUpdating = False

' Get rid of any spaces between paragraph marks

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(^13)( {1,})(^13)"
.Replacement.Text = "\1\3"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Run above code again because I can't otherwise get the second para
mark in the
' first running of the code to be the first para mark in the second
running
With Selection.Find
.Text = "(^13)( {1,})(^13)"
.Replacement.Text = "\1\3"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Delete extra paragraph marks.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute Replace:=wdReplaceAll

' clear Find box
With Selection.Find
..Text = ""
..Replacement.Text = ""
..MatchWildcards = False
End With

End Sub

Also, if you're interested, here is a macro that deletes empty paragraph
marks at end of doc, leaving just the end of document paragraph mark.

Sub EmptyParasAtEndDelete()
' Deletes empty paragraph marks, linebreaks, spaces, tabs and squares
' at end of document then returns cursor to starting point.

Dim r1 As Range, r2 As Range
Set r1 = Selection.Range
Selection.EndKey wdStory
' Extend selection over final para mark
Selection.EndKey wdStory, wdExtend
Set r2 = Selection.Range
r2.MoveStartWhile cset:=vbCr & vbTab & " " & Chr(11) & Chr(26), _
Count:=wdBackward
r2.Delete
r1.Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top