![]() |
|
#1
|
||||
|
||||
![]()
I just stumbled upon a feature that I hope is a formal part of Word.
I prepare a lot of contracts and other legal documents. We have templates for most of them. In each template, we have some "terms" that we have coded as "variables" by prefixing a special character like "&" or "$" so we can clearly identify them and distinguish them from regular text. For example, many of the documents specify the role the attorney is to assume, such as "mediator" or "special master". In those documents, in place of the actual role, we put the text "&role". The problem is that this term may occur in the middle of a sentence (where it will be all lower case "mediator"), at the start of a sentence (where the first letter must be capitalized "Mediator"), or in a title or heading (where it must be in all caps "MEDIATOR"). Up to today, I had been putting three different variables in the document (&role, &Role, and &ROLE) doing a replace three times with the Match Case option selected. Today, quite by accident, I did a Replace All on a string that occurs in the document in both upper and lower case. To my surprise, Word matched the case in the replacement. If the target was all lower case, the replacement was all lower case. If the target was all upper case, so was the replacement. After doing a little experimentation, it appears that the rules are these:
Is this about right? This isn't working for me because of the leading "&". If I move that to the end (role&), it should work. I just tested it on one document and it seems to work. Is there anything more I need to know or be aware of? Last edited by Jennifer Murphy; 02-10-2013 at 12:38 AM. Reason: Added question. |
#2
|
||||
|
||||
![]()
Hi Jennifer,
Here's a macro that should free you from the std F/R constraints: Code:
Sub Demo() Dim StrRep As String, StrLeft As String, StrRight As String, i As Long With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = InputBox("What is the text to Find") .Replacement.Text = "" StrRep = InputBox("What is the Replace text") StrLeft = Left(StrRep, 1) StrRight = Right(StrRep, Len(StrRep) - 1) .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found i = i + 1 With .Duplicate If .Words.First = .Sentences(1).Words.First Then .Text = UCase(StrLeft) & StrRight Else .Text = StrLeft & StrRight End If End With .Collapse wdCollapseEnd .Find.Execute Loop End With MsgBox i & " instances replaced." End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Question about Case statement | Jennifer Murphy | Word VBA | 1 | 01-05-2013 02:30 PM |
![]() |
tinfanide | Excel | 2 | 09-13-2011 05:08 AM |
Case Sensitive (contains) Selection | apolloman | Excel | 2 | 07-12-2011 04:50 AM |
![]() |
davers | Word | 1 | 04-30-2009 12:41 PM |
Upper to lower case | jd | Excel | 1 | 04-28-2006 07:40 AM |