• VBA Find Hidden Text Won’t! (Word 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » VBA Find Hidden Text Won’t! (Word 2003)

    Author
    Topic
    #437217

    I seem to have a situation where the Edit/Find function won’t locate hidden text in (apparently) a particular document or kind of document. Here are the specifics. (I’m sure this will be like taking my car to the mechanic, but I’m going to give it a shot anyway!) We create a type of document in either English or Japanese. Both of them contain “author text,” which is text intended for the auhor’s edification but which should ultimately be removed. This text is formatted Hidden and Blue. To remove the text, I wrote what would seem to be a VERY simply routine that I’ve used a thousand times. It uses Find/Replace. I tried using just the Find and then deleting the Found range, but the problem apparently is that it can’t FIND anything to delete! Here is the code:

    Sub RemoveHiddenText()
    If MsgBox(“Are you sure you want to remove all hidden Author Notes?”, vbQuestion + vbOKCancel, “Remove Author Notes”) = vbOK Then
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Font.Hidden = True
    .Font.Color = wdColorBlue
    .Execute replacewith:=””, Replace:=wdReplaceAll
    End With
    End If
    End Sub

    I even cleared the formatting arguments, even though I understand that this is not (was not?) a “sticky” setting when using the Range object. Yet if I launch Word and immediately open a document written by our Japanese partners, the code (almost inevitably) fails to work; it can’t find the hidden text, let alone replace it. It also seems to inevitably fail if I then try it on an English document during the same Word session. BUT if I launch Word and then immediately open a document in English, it WILL work; and if I then open the Japanese document (in the same Word session,) then it seems always to work in THAT document, too!

    I know that things that “usually” work or don’t work are the hardest to troubleshoot, but I was hoping that someone may have had a similar experience, and could perhaps steer me towards a solution or an explanation. I am attaching an edited version of the document here so that you can test it. (Remember it fails to work only if the Japanese document is the first document you try it on during that Word session.) I prefer to avoid using the Selection object (the Range object is more graceful), but I’m prepared to switch to that, if necessary. (Note that as I prepare to post this message, I did another test to guarantee that I was able to duplicate the problem, and of course, it seems to work flawlessly. Nonetheless, if I don’t post this, it will cease to work tomorrow and I’ll have to start all over again! Please HELP!)

    Viewing 1 reply thread
    Author
    Replies
    • #1038724

      Could the text not be showing at the time the macro runs? You could try this before the search:

      ActiveDocument.ActiveWindow.View.ShowHiddenText = True

      to rule out that possibility.

    • #1038802

      It’s a bug that was discussed somewhere on the Lounge before, I think.

      IIRC, it depends on whether the first character in the document is hidden or not?

      One work-around was to use .Font.Hidden = wdToggle (instead of = True). Not really good code, but simple.

      cheers Klaus

      • #1038818

        Thbbbt! I guess I should have researched that before I demo-ed my app in Japan! (“Oh, sorry! I think I may not have finished that function. I’ll get right on it!”)

        I’m assuming that it works precisely because the settings are (still) NOT sticky and it’s toggling it on?

        • #1039223

          > I’m assuming that it works precisely because the settings are (still) NOT sticky and it’s toggling it on?

          Yes, something like that. Word seems to be confused about what’s hidden and what’s not when the first character is hidden, and wdToggle is just something that seems to work around that bug.
          If you are writing a professional app, you might do something more elaborate to work around it (… say verify that what you think you found is hidden text really is).

          The work-around would probably blow up in your face if MS ever decides to fix the bug. Not that that is too likely…

          cheers Klaus

    Viewing 1 reply thread
    Reply To: VBA Find Hidden Text Won’t! (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: