genespos
genespos

Reputation: 3311

Selection.Find.Execute doesn't work correctly with Word 2013

I wrote a macro for Word 2003 to remove undesired "carriage return" and replace them with spaces.

So, after selectig the piece of text, I run the macro and the "carriage return" in the selection were replaced.

But under Word 2013, even if I select only a few rows, the replecement take place throughout the document.

This is my code:

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
With Selection.Find
    .Text = "  "
    .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
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll

EDIT

I've tried with the macro recorder and I've got the same code. Furthermore the command worked on the selection during recording but throughout the document while executing the macro.

Is this a bug? Is there any workaround?

Upvotes: 3

Views: 5173

Answers (1)

RealCheeseLord
RealCheeseLord

Reputation: 795

The problem could probably be .Wrap = wdFindAsk, since this enables a prompt after the search of your Selection which asks you wether the search should be extended to the whole Document. But if you run the macro, the prompt does not appear and defaults to Search the whole Document, so your whole Document is checked.

.Wrap = wdFindStop should disable the prompt, and stop your replacement after the end of your Selection is reached.

See: Wrap property

Upvotes: 1

Related Questions