Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Stop FindReplaceAnywhere from putting all caps in headers

Posted on 2009-07-13
Medium Priority
Last Modified: 2012-05-07
I'm using the FindReplaceAnywhere script from the following site:

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.

Question by:Cory Vandenberg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 76

Expert Comment

ID: 24843281
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).
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24843470
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.

LVL 18

Author Comment

by:Cory Vandenberg
ID: 24844393
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.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 18

Author Comment

by:Cory Vandenberg
ID: 24844425
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
    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"
        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"
            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
  lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
  For Each rngStory In ActiveDocument.StoryRanges
      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
        End If
      Case Else
      End Select
      On Error GoTo 0
      Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
End Sub
Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)
  With rngStory.Find
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End Sub

Open in new window

LVL 76

Accepted Solution

GrahamSkan earned 2000 total points
ID: 24844702
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.
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24844889

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.

LVL 18

Author Closing Comment

by:Cory Vandenberg
ID: 31602979
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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (, I explained how to get a free, one-month trial of Office …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question