Why does this vba replacetext macro not work?

A

Angus

Here is the macro:
Sub convertPtoBR()
'
' convertPtoBR Macro
' remove full new para and insert break - less line spacing
'
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

But if I select a section of text (with target formatting) and run
macro nothing changes? What am I doing wrong?

Yesterday the macro worked but not today? How do I make it work
reliably?

Angus
 
G

Graham Mayor

Whether or not it works, it is certainly not the correct approach to remove
inter-paragraph spacing. Apply a style to the selection that has no space
before/after
If you must apply manual formatting with a macro

Dim oRng As Range
Dim oPara As Paragraph
Set oRng = Selection.Range
For Each oPara In oRng.Paragraphs
oPara.SpaceAfter = 0
oPara.SpaceBefore = 0
Next oPara

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Incidentally your macro should work as far as it goes, but there were a
couple of lines missing

With Selection.Find
.ClearFormatting 'Add
.Replacement.ClearFormatting 'Add
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindStop 'Change
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll 'Change
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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