Consulting

Results 1 to 6 of 6

Thread: Solved: Finding Text Within Square Brackets

  1. #1
    VBAX Regular
    Joined
    Sep 2004
    Posts
    7
    Location

    Solved: Finding Text Within Square Brackets

    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

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Are they bookmarks, Jim? Or just straight characters?

    If form fields (with bookmarks):

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

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi jimoo,

    Try this:

    [VBA]
    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
    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Regular
    Joined
    Aug 2004
    Location
    On a 100 acre hobby farm in beautiful west Quebec.
    Posts
    87
    Location
    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.
    Eric

    Experience is not what happens to a man; it is what a man does with what happens to him. ? Aldous Huxley

  5. #5
    VBAX Regular
    Joined
    Sep 2004
    Posts
    7
    Location
    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.

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    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.
    ~Anne Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •