Microsoft Office 2016 - How to apply Macro for selected text only?

0

I created Macro for replacing multiple characters at once. If I apply it for the whole document and then paste another text into the same document, applying the same Macro will do fine for newly pasted text, but will mess up rest of the text unto which Macro was already applied.

I was thinking problem would be solved if Macro could be applied only for selected text, leaving the rest of the document untouched.

Help, anyone?

Regards, Tom

Tomislav Lemal

Posted 2017-12-08T12:18:35.947

Reputation: 21

Can you show the source code of your macro? The solution may depends on how do you work with selection object in the macro. – duDE – 2017-12-08T12:28:17.587

This is the code: http://textuploader.com/dq37a Thanks!

– Tomislav Lemal – 2017-12-08T13:12:32.207

Answers

1

Change .Wrap = wdFindContinue to .Wrap = wdFindStop in your macro:

' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "ä"
        .Replacement.Text = ChrW(257)
        .Forward = True
        .Wrap = wdFindContinue

duDE

Posted 2017-12-08T12:18:35.947

Reputation: 14 097

It's working only when I select the text. But if text is not selected (mouse cursor blinking) then it applys for whole document starting from the point where the mouse cursor is (no text selected) – Tomislav Lemal – 2017-12-08T13:19:11.990

Did not you mean, "I was thinking problem would be solved if Macro could be applied only for selected text" ? – duDE – 2017-12-08T13:20:54.120

well... it would, but a little upgrade would be nice :) – Tomislav Lemal – 2017-12-08T13:22:38.203