April 9, 2024
Contribute to the Microsoft 365 and Office forum!
Click here to learn more 💡
May 10, 2024
Word Forum Top Contributors:
Find and replace only selection in VBA
' ReplaceParaMarks
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Report abuse
Thank you.
Reported content has been submitted
You've discovered some of the fickleness of the .Find object. See:
http://gregmaxey.mvps.org/word_tip_pages/words_fickle_vba_find_property.html
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngLimit As Word.Range
Set oRng = Selection.Range
Set oRngLimit = oRng.Duplicate
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute
If oRng.InRange(oRngLimit) Then
oRng.Text = " "
oRng.Collapse wdCollapseEnd
Else
Exit Sub
End If
Wend
End With
End Sub
***
Death smiles at us all, but all a man can do is smile back.
For more help with Word visit:
http://gregmaxey.com/word_tips.html
Report abuse
Thank you.
Reported content has been submitted
3 people found this reply helpful
·Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
You've discovered some of the fickleness of the .Find object. See: http://gregmaxey.mvps.org/word_tip_pages/words_fickle_vba_find_property.html
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngLimit As Word.Range
Set oRng = Selection.Range
Set oRngLimit = oRng.Duplicate
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute
If oRng.InRange(oRngLimit) Then
oRng.Text = " "
oRng.Collapse wdCollapseEnd
Else
Exit Sub
End If
Wend
End WithEnd Sub
Thanks a lot! And it works for replacing.
what if replacing with a ""?
I want to delete all "^p" within the selected area instead of the whole document.
And it DOESN'T WORK even I tried to substitute " " with "".
Report abuse
Thank you.
Reported content has been submitted
1 person found this reply helpful
·Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Question Info
Last updated February 12, 2023 Views 13,311 Applies to: