Can this be done with a macro? If so, what would that macro look like?

I’m working on a large document and would like to be able to select several different sections of text, press a button or two, and have strings of text appear both before and after the selected text. As an example of what I’m after:

Step 1.

In this sentence, I select this bit of text and then use ctrl to also select this second bit of text and this third bit of text.



Step 2.

I enter a command that executes a macro.



Result:

In this sentence, I select < desired string of before text > this bit of text < desired string of after text > and then use ctrl to also select < desired string of before text > this second bit of text < desired string of after text > and < desired string of before text > this third bit of text < desired string of after text >.



I should note that I have AutoText commands set up that can be used to generate the desired text strings, but relying solely on those will take days of tedious repetition to get the desired results. I’m hoping a macro can drastically reduce the time needed to accomplish this, but I do not possess the knowledge or understanding necessary to create such a macro. Is it possible to write a macro that can do this? If so, what would that macro look like?

No need for Makro, do Step1 and look at my Answer.

Hi.

Search and replace
Search (.*).
Replace with textbefore $1 textafter.
with Options: [✓]regular Expressionand[✓] Selection only
and press the obvious Replace All Button

Sorry for the wrong answer above, I’ve missed the point that [x] selection only is not available in multiselection-mode.

So try this:

Sub Main

Textbefore = "put in here whatever you want"
Textafter =  "and here also"

doc = thisComponent
selections = doc.CurrentSelection

for i = 1 to selections.Count-1
	selections(i).String = Textbefore & selections(i).String & Textafter
next


End Sub

There is currently no text before or after the desired sections. I was hoping to create a macro that would add one text string to the beginning of selected sections of text and add a different text string to the ends of those sections.

Thats exactly whats my suggested search & replace procedure does.
any Questions?

Doing that replaces the selected text with the string intended to go in front of it. What I’m trying to do is this:




Take the following sentence:




“Attach the flibbertyjibbit to the thing-a-ma-whatsit and use the resulting rejimotus to confuse a cat.”




Select the words flibbertyjibbit,thing-a-ma-whatsit, and rejimotus in the sentence, enter a single command, and get this result:




“Attach the < desired string of before text >flibbertyjibbit< desired string of after text > to the < desired string of before text >thing-a-ma-whatsit< desired string of after text > and use the resulting < desired string of before text >rejimotus< desired string of after text > to confuse a cat.”

Please see my edited answer, sorry for the misleading advice.