Find and repalce a specific word within parentheses

Posted on 2011-04-29
Last Modified: 2012-08-13
I need to replace a specific word within closed parentheses in MS word 2010 with another word. I can find any character within parentheses using wildcards in FR : \(*\)

Then, what I need to do is find exactly the word "and" and replace it with an ampersand character "&". Not the string "and" but the specific word "and", which is always preceded by and followed with one or more words (in other words, is always [single space]"and"[single space] (or more generally [whitespace]"and"[whitespace]).

Also, the expression \( and \) doesn't work since it find all instances of the pattern whether or not it is between closed parentheses, if that makes any sense. \(*[ and ]*\) selects the entire phrase, e.g., "(you and I)", not just the word "and".

Sorry, I don't know how "storage zone" was selected, I meant the question to only be in MS Word.

Question by:orerockon
    LVL 76

    Expert Comment

    With 'Use wildcards' ticked, try

    Find:  (\(*)<and>(*\))

    Replace: \1&\2
    LVL 1

    Expert Comment

    Use the space befor and after the word and as well as the replacement ampersand.

    Find: and
    Replace with: &


    Author Comment

    Graham: It works, but it doesn't find only " and " within closed parentheses: "( and )". For example, it also finds:

    "(A) and (B) " so it is finding " and " between two sets of closed parentheses, not within one set.
    LVL 76

    Expert Comment

    Wildcard Finds cannot always be guaranteed not to find spurious examples. It is often a matter of tuning to the best fit.

    I will do some more experiments.
    LVL 76

    Accepted Solution

    I cannot find a way of doing it in a single Find.

    Here is a VBA macro instead.
    Sub ToAmpersand()
        Dim rng As Range
        Dim rng2 As Range
        Set rng = ActiveDocument.Range
        With rng.Find
            .MatchWildcards = True
            .Text = "\(*\)"
            Do While .Execute
                Set rng2 = rng.Duplicate
                With rng2.Find
                    .MatchWildcards = True
                    .Text = "<and>"
                    .Replacement.Text = "&"
                    .Execute Replace:=wdReplaceAll
                End With
        End With
    End Sub

    Open in new window


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
    This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
    This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
    This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now