Find and replace operation with a selection object instead of rangeobject

A

andreas

Below code will remove trailing spaces from all paragraphs in the
current document, such as ...

sample text space space paragraph mark

Sub RmvTrSpPa()
With ActiveDocument.range.Find
..Format = False
..Text = "^w^p"
..Replacement.Text = "^p"
..Execute Replace:=wdReplaceAll
End With

I rewrote the macro to do the search and replace operation just in the
selected area, but I am getting a runtime error "5692" and .Execute
Replace gets highlighted Yellow . What's wrong with below code? Help
is appreciated. Thank you very much in advance.

Sub RmvTrSpPa_Selection()
'This will remove trailing spaces from all paragraphs in the selection
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
..Format = False
..Text = "^w^p"
..Replacement.Text = "^p"
..Execute Replace:=wdReplaceAll
End With
End Sub
 
D

Doug Robbins - Word MVP

That code runs, and does what's intended, without error here.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

andreas

That code runs, and does what's intended, without error here.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP










- Zitierten Text anzeigen -

Hey Dough,

thank you very much for your quick reply. I now found out what was
wrong with this code. When I selected "Search and Replace" on the
"Edit-menu" I noticed that "Match Wildcards" was still checked. I
unchecked it and then it ran as desired. Hence I should have added
".MatchWildCards = False". Then it would have run right from the
beginning.

Thank you for your help. Regards, Andreas
 

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