Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Find Hidden Text Won't! (Word 2003)

    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!)
    Attached Files Attached Files
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  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: VBA Find Hidden Text Won't! (Word 2003)

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

    <code>ActiveDocument.ActiveWindow.View.ShowHiddenT ext = True</code>

    to rule out that possibility.

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

    Re: VBA Find Hidden Text Won't! (Word 2003)

    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.

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

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Find Hidden Text Won't! (Word 2003)

    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?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

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

    Re: VBA Find Hidden Text Won't! (Word 2003)

    > 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...

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

Posting Permissions

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