Solved

Stop FindReplaceAnywhere from putting all caps in headers

Posted on 2009-07-13
7
369 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:WarCrimes
  • 4
  • 2
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
 
LVL 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
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
 
LVL 18

Author Comment

by:WarCrimes
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Author Comment

by:WarCrimes
Comment Utility
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
 
LVL 18

Author Comment

by:WarCrimes
Comment Utility
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
 
LVL 18

Author Closing Comment

by:WarCrimes
Comment Utility
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now