Find and replace only selection in VBA

I have a Word 2010 document in which I want to find and replace paragraph characters with a space. The code works except I want it to only replace in the selected text. When I run it it replaces all the paragraph marks on the page, including text that I have not highlighted. What needs to be changed in my code?  Thank you for your help.

    ' 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

Answer
Answer

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
   

Greg Maxey
***
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

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.

Answer
Answer

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
   

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 "".

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: