Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2013, 12:29 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Replace & case Windows XP Replace & case Office 2007
Competent Performer
Replace & case
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Replace & case

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:
  • The Find What and Replace With fields must be all lower case.
  • These fields and the target text in the document must begin with a letter.
If these conditions are met, the Replace function will match the case of the first letter and the case of the rest of the word separately.

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.
Reply With Quote
  #2  
Old 02-11-2013, 03:26 AM
macropod's Avatar
macropod macropod is offline Replace & case Windows 7 64bit Replace & case Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Capitalisation of the replacement text remains the same as whatever you input unless it starts a sentence, in which case the first letter is capitalised.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Case statement Jennifer Murphy Word VBA 1 01-05-2013 02:30 PM
Replace & case Custom Text Filter for this particular case tinfanide Excel 2 09-13-2011 05:08 AM
Case Sensitive (contains) Selection apolloman Excel 2 07-12-2011 04:50 AM
Replace & case From all UPPER CASE to Proper Case davers Word 1 04-30-2009 12:41 PM
Upper to lower case jd Excel 1 04-28-2006 07:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft