PDA

View Full Version : Help with macro - find bracket



Trewert
06-04-2008, 08:07 PM
Any help with a macro I am trying to create would be great as I am a beginner to VBA and currently feeling my way through.
I have managed to create a macro to find each instance of [ ...text... ] throughout a document. However I become stuck if there are brackets within brackets ie. it always finishes at the first instance of ] instead of the one I want.
Is there a way that this can be done.I could have totally gone about this in the wrong way but this is what I have created so far.
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
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
End Sub
I will get myself a book and learn this but in the meantime any help is really appreciated.
Thanks

Tinbendr
06-05-2008, 10:37 AM
Check out this code (http://vbaexpress.com/forum/showpost.php?p=146366&postcount=11) I wrote for a similar post.

It's a little more than you want, but you should be able to adapt it.

fumei
06-06-2008, 08:37 AM
"It's a little more than you want"

Interesting. Actually, it is quite less than what is wanted.

In your example:

^Colors:Red,Blue,Green,Purple^

If you add a nested ^ - which is what is being asked for -

^Colors:Red,^Blue^,Green,Purple^

The code will never find anything but Red.