Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Mar 2002
    Location
    Omaha, Nebraska
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Odd Word Find/Replace Behavior (Word 2000 SR-1)

    I'm experiencing an odd occurrence w/ Word's Find/Replace functionality. Everything works fine if I run Find/Replace manually (through Word's dialog boxes), but if I run the following code (and provide it the appropriate Range ... i.e., objDoc.StoryRanges(wdMainTextStory) ...) things start to break. (And I'm not completely sure it isn't my document...) Anyway, If I run the code on the attached document everything works fine, however, if I start with a fresh copy of the document, change the 1st line of text in the document to an 'Optional Paragraph' style (defined as: Normal + Font:Italic, Hidden, Font color: Blue) and run the code, Word's Find/Replace does the exact opposite of what it's supposed to. (It removes everything that isn't 'hidden' and leaves all of the 'hidden' text. Any Ideas?

    I have found that if I comment out the line regarding ' .Replacement.Font.Hidden = False' then everything works fine all the time. But why would this cause a problem???

    '****************************************
    Private Sub ReplaceHiddenText(oRange As Range)
    Const TMP_TAG As String = ">>ZAP<<"


    On Error Resume Next

    ' Setup the seach criteria for hidden text
    ' stuff document with a temperary place holder
    oRange.Collapse direction:=wdCollapseStart
    oRange.Find.ClearFormatting
    oRange.Find.Replacement.ClearFormatting
    With oRange.Find
    .Font.Hidden = True
    .Text = vbNullString
    .Replacement.Text = TMP_TAG
    .Replacement.Font.Hidden = False
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    oRange.Find.Execute Replace:=wdReplaceAll

    'replace the placeholder with nothing
    oRange.Collapse direction:=wdCollapseStart
    oRange.Find.ClearFormatting
    oRange.Find.Replacement.ClearFormatting
    With oRange.Find
    .Text = TMP_TAG
    .Replacement.Text = vbNullString
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    oRange.Find.Execute Replace:=wdReplaceAll
    End Sub
    '*******************************
    Attached Files Attached Files

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Odd Word Find/Replace Behavior (Word 2000 SR-1)

    My reference to "first paragraph" in the above points really should say "contiguous range of paragraphs in the same style commencing with the first paragraph."

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Odd Word Find/Replace Behavior (Word 2000 SR-1)

    I have the same version of Word that you do, and I get the problem of the Hidden font attribute reversal only if:
    1. <LI>The first paragraph of the target range is in a paragraph style that contains the Hidden attribute and
      The insertion point is anywhere in that first paragraph;

      <LI>A character style containing the Hidden attribute has been applied to the entire first paragraph of the target range (including or not including the paragraph symbol at the end of the paragraph itself) and
      The insertion point is anywhere in that first paragraph; or

      <LI>A character style containing the Hidden attribute has been applied to the beginning of the first paragraph of the target range and
      The insertion point is immediately to the left of any character with that style.
    Accordingly, the universal workaround, assuming that there is some text that does not bear the problematic style, is, prior to executing the search:

    Selection.EndKey unit:=wdStory

    Definitely sounds like a bug!!!

    <center><img src=/w3timages/blueline.gif width=33% height=2></center>
    <font color=blue>Correction</font color=blue>: It's not really universal. You need to have the insertion point in a paragraph that doesn't have a problematic style applied to it. Guess it will take a bit more futzing to find such a paragraph, or you can add code to insert a "clean" empty paragraph at the new insertion point to work around that:

    <pre>With Selection
    .EndKey unit:=wdStory
    .TypeParagraph
    .ParagraphFormat.Style = wdStyleNormal
    End With</pre>


    And I guess it needs to come out again after the search. What a PITA.

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Odd Word Find/Replace Behavior (Word 2000 SR-1)

    I ran into the same bug last year.

    My work-around after some experiments was to replace
    .Font.Hidden = True
    with
    .Font.Hidden = wdToggle

    This seems to work reliably in all cases, though I would add a comment why this funny construct is needed (-- Don't you hate it when you have to write crazy code because straight code won't work <img src=/S/question.gif border=0 alt=question width=15 height=15>)
    BTW, I wouldn't use <font face="Georgia">On Error Resume Next</font face=georgia> unless you have a very good reason <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #5
    New Lounger
    Join Date
    Mar 2002
    Location
    Omaha, Nebraska
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Odd Word Find/Replace Behavior (Word 2000 SR-1)

    Thanks to both Klaus and JScher2000. Both of these solutions appear to work for me. In an effort to keep the code as simple as possible, I'm going to put some comments in about the 'odd behavior' and use the wdToggle variable.

    I agree, having to write 'workaround' code for something that should work is very tedious and crazy.

    Thanks Again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •