Selecting and Using Text in VBA

I want to determine (and then manipulate the text string between an open bracket ("[}) and a close bracket ("]").  Since I want to replace the bracket and the text, I also need to be able to delete that, hopefully in a single selecting process.

The bracketed material (text) is within a paragraph and not within a bookmark or anything so this has to be specific.  I assume that I will have to put the cursor inside the text, search backwards for the open bracket and then somehow do an extended selection/search to the close bracket.

How is that done?

TIA
Answer
Answer

Put the cursor between the brackets:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = Selection.Range
oRng.MoveStartUntil CSet:="[", Count:=wdBackward
oRng.Start = oRng.Start - 1
oRng.MoveEndUntil CSet:="]", Count:=wdForward
oRng.End = oRng.End + 1

oRng.Select
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

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 October 5, 2021 Views 1,146 Applies to: