Stop FindReplaceAnywhere from putting all caps in headers

I'm using the FindReplaceAnywhere script from the following site:
http://word.mvps.org/faQs/Customization/ReplaceAnywhere.htm

The issue I'm having is, that although this does replace all occurrences of the old text with the new text, inside of the header, it is making the text all caps, and I need it to preserve the capitalization from the input box.

When I try to step through the debugger, the header cases don't trigger because of the all_caps I'm guessing, but removing any breakpoints, they get replaces, with no case-sensitivity.

Any ideas how to preserve formatting?  The replacements in the body are fine.

WC
LVL 18
Cory VandenbergSenior Risk ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
I can't reproduce that symptom.

There is no setting for .MatchCase in the actual Find object code, so it defaults to False, which means that the case setting is ignored in the Find, but the replacement should copy the case of the found text (as far as possible).
0
Joanne M. OrzechManager, Document Services CenterCommented:
Perhaps the header has a style applied that is set to All Caps under the font attributes?  If you click in the text that is turning All Caps, go to Format, Styles, and click in the dropdown arrow in the top box on the right hand styles panel, go to Modify, Font, and see if it's set to All caps there.

0
Cory VandenbergSenior Risk ManagerAuthor Commented:
There is a style applied to the header, but it is not set to all caps.  I made sure to check that before posting the question.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Cory VandenbergSenior Risk ManagerAuthor Commented:
I have a custom menu with an item whose OnAction property is set to the ChangeProjName sub, which then calls the other subs listed below.

Thought posting it might help reproduce what's going on.
Sub changeProjName()
    Dim newName As String
    Dim bk As Range
    Dim oldName As String
    
    Application.ScreenUpdating = False
    On Error Resume Next
    Err.Clear
    Set bk = ActiveDocument.Bookmarks("BM_Project_Name").Range
    If Err <> 0 Then
        MsgBox prompt:="There is not a bookmark named 'BM_Project_Name'." & vbCrLf & vbCrLf & _
            "Please set up the proper bookmark.  See the About file for how to do this.", buttons:=vbOKOnly, _
            Title:="No Project Bookmark"
    Else
        newName = InputBox(prompt:="What would you like to change the Project Name to?", _
                Title:="Change Project Name")
        If StrPtr(newName) = 0 Then
            Exit Sub
        ElseIf newName = "" Then
            MsgBox "Please enter a Project Name"
        Else
            oldName = ActiveDocument.Bookmarks("BM_Project_Name").Range.Text
            Call UpdateBookmark("BM_Project_Name", newName)
            Call FindReplaceAnywhere(oldName, newName)
        End If
    End If
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
    Dim BMRange As Range
    Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
    BMRange.Text = TextToUse
    ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
 
Public Sub FindReplaceAnywhere(pFindTxt As String, pReplaceTxt As String)
  Dim rngStory As Word.Range
  Dim lngJunk As Long
  Dim oShp As Shape
TryAgain:
  lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
  For Each rngStory In ActiveDocument.StoryRanges
    Do
      SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
      On Error Resume Next
      Select Case rngStory.StoryType
      Case 6, 7, 8, 9, 10, 11
        If rngStory.ShapeRange.Count > 0 Then
          For Each oShp In rngStory.ShapeRange
            If oShp.TextFrame.HasText Then
              SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt
            End If
          Next
        End If
      Case Else
      End Select
      On Error GoTo 0
      Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
  Next
End Sub
 
Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)
  With rngStory.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End Sub

Open in new window

0
GrahamSkanRetiredCommented:
Sorry, I still can't reproduce it.

A few points, though.

1. You could consider using REF fields to echo the bookmarked text and then do an Activedocument.Fields.Update

2. If you want to test for the presence of a bookmark, you can use the Exists method:
IF ActiveDocument.Bookmarks.Exists("BM_Project_Name") Then

3. And one from a pedant: the Find and Replace format settings are only remembered for the Selection object. If you are using a Range object, Clearformatting is not needed.



If the anomalous behaviour happens on any system, then the document may be corrupt. Otherwise it might be your Word installation. Document corruption can often be cleared by copying the whole document and pasting it to a new one.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cory VandenbergSenior Risk ManagerAuthor Commented:
Graham,

1. I have thought about this, which was the reason for previous questions I have posted.  The issue is the user may edit the header, and in doing so, delete the Ref field.  Also, there is a stand-alone version of the template that would not include a title page where the bookmark is set-up prior, so a Ref field would have nothing to look at until the user defined the bookmark.

2.Yes, thanks for reminding me.  I actually ran across that property doing something else, but did not go back and change it for the condition at the beginning.  Doing that now to clean it up and make it more readable.

3.  Ya, I noticed when I commented out the ClearFormatting that nothing was different, so I figured they weren't necessary.  Thanks for confirming.

I am going to try on another machine tomorrow, and also on mine with a separate document.  I'll let you know how it goes.

WC
0
Cory VandenbergSenior Risk ManagerAuthor Commented:
The templates I use have the default names in all caps, although the style allows lowercase letters.  For some reason using replace then changes the new text to all caps as well.  When I updated the templates so the text used both upper and lower case, the replace works as expected.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

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.