Solved

Stop FindReplaceAnywhere from putting all caps in headers

Posted on 2009-07-13
7
381 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:Cory Vandenberg
[X]
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
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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).
0
 
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.

0
 
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.
0
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
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
    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
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.
0
 
LVL 18

Author Comment

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

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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.

623 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