#1
|
|||
|
|||
Find and Replace "find what text"
In a long list of references, I'm trying to find each instance of a calendar year and replace it with that same year enclosed by parentheses. For example, find "1991" and replace with "(1991)"; find "2007" and replace it with "(2007)". I have no problem finding the years, but can't seem to crack the code on replacing and inserting the parens. |
#2
|
||||
|
||||
You could use a wildcard Find/Replace, where:
Find = <[0-9]{4}> Replace = (^&) The < and > tell Word the number can't be part of a larger string (e.g. 5 digits), whilst the [0-9]{4} tells Word to find a 4-digit string. The Replace expression tells Word to insert ( and ) either side of the found content, which is indicated by the ^&.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
OK, thanks. I see that works when I turn "Track Changes" off. When "Track Changes" is on, it returns the year followed by (). Not sure why, but am happy with the fix. Thanks again!
|
#4
|
||||
|
||||
Your original post didn't mention anything about track changes. To work with those, you'd need to use a macro that, instead of replacing the found string, inserts the brackets before & after:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "<[0-9]{4}>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found .InsertBefore "(" .InsertAfter ")" .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
OK, thanks again
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with the "find and replace" option; character problem | martinn4 | Word | 3 | 02-20-2014 03:52 AM |
can find and replace instances of the "enter" key? | snunicycler | Word | 7 | 05-02-2013 11:54 PM |
Word Macro to find and delete rows that contain adjacent cells containing "." | AlexanderJohnWilley | Word VBA | 7 | 11-08-2012 10:15 AM |
Is there a way to use "find/replace" to find italics words? | slayda | Word | 3 | 09-14-2011 02:16 PM |
Find & Replace words with "/" prefix & suffix | tollanarama | Word | 4 | 01-25-2011 02:19 AM |