PDA

View Full Version : Solved: Finding Text Within Square Brackets



jimoo
09-05-2004, 07:05 PM
I would like to spin thru the document and find all the items within brackets.

Does anyone have an idea how to do this? You can display the results in a messagebox comma delimited, or better yet with a CRLF.

Document Sample:



Dear [salutation]

We are happy to report that your stock portfolio is now worth [value] and that you have joined the [clubname] club.

Please contact us immediately for further assistance.

Sincerely,



[signature]



Expected Results with messagebox:



salutation,value,clubname,signature

Anne Troy
09-05-2004, 09:13 PM
Are they bookmarks, Jim? Or just straight characters?

If form fields (with bookmarks):

http://www.vbaexpress.com/kb/getarticle.php?kb_id=153

TonyJollans
09-06-2004, 01:40 AM
Hi jimoo,

Try this:


Sub FindBracketedText()

Dim msg As String

With Selection

.HomeKey wdStory

With .Find

.ClearFormatting
.Format = False

.Text = "\[*\]"
.Replacement.Text = ""
.MatchWildcards = True

.Forward = True
.Wrap = wdFindStop

.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False

.Execute

End With

Do

msg = msg & Mid$(Selection, 2, Len(Selection) - 2) & vbNewLine
.Find.Execute

Loop While .Find.Found

End With

MsgBox msg

End Sub

EricFletcher
09-06-2004, 08:56 AM
Jimoo: You can do this in Word without VBA too. Open the Find dialog and turn on the wildcard option; in Find what, put "\[*\]" (without the quotes), then select "Highlight all items found in" and choose "Main Document". Click Find All and each instance of the square brackets and everything within them will be highlighted. Drop out of the Find dialog and Copy. Go to the end of the file and Paste. You'll have a list of all the items, one per line.

Hmm... when I re-read Tony's code, I think it is pretty much the same idea as what I'm suggesting. Use his for within VBA but be aware of my technique for ad hoc stuff.

jimoo
09-06-2004, 10:27 AM
Thanks Dreamboat, Eric, and especially Tony. Tony?s example is exactly what I am looking for.

To give you a little more background. I have a Visual FoxPro (VFP) application that will open an existing Word Document and replace specific values with values from the VFP table.

I started to out my journey focusing on either fields or bookmarks. I found fields difficult to use since the document was looking for it to be tied to a data-source, and in this particular example the data-source was not connected to Word, but being fed to Word from VFP.

I then toyed with bookmarks, and dreamboat provided me with a great example that I got to work perfectly. However, what if the same data item (bookmark name) needed to appear more than once in the letter? Bookmarks, by nature, can only exist once (per bookmark name) in a document.

Then I decide to try using data separated by delimited text, and in this case I chose to use the [ ] combination. Using this method, the delimited text can appear more than one time because it is not being used by the document (such as a field or bookmark) and the search and replace will take care of multiple instances.

Finally, the whole idea of doing it this way is to allow the user to create any letter, and enclose the data items inside [ ]. My program will determine which data items they selected, and using a lookup table retrieves the proper values from the VFP tables.

Anne Troy
09-06-2004, 10:35 AM
Hi, Jim!

While bookmarks with the same name can only be used once, you can refer to a bookmark by using (the manual method, at least is) Insert-Cross reference-To a bookmark.

You may need to update your document if you use this method so that the cross-ref field becomes populated. To update: Ctrl+A, then F9.