Solved

Stop FindReplaceAnywhere from putting all caps in headers

Posted on 2009-07-13
7
379 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

821 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