• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Find and repalce a specific word within parentheses

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.

0
orerockon
Asked:
orerockon
  • 3
1 Solution
 
GrahamSkanRetiredCommented:
With 'Use wildcards' ticked, try

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

Replace: \1&\2
0
 
ScownCommented:
Use the space befor and after the word and as well as the replacement ampersand.

Find: and
Replace with: & 

0
 
orerockonAuthor Commented:
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.
0
 
GrahamSkanRetiredCommented:
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.
0
 
GrahamSkanRetiredCommented:
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
        Loop
    End With

End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now