none
Font.Hidden = 1 doesn't work as documented RRS feed

  • Question

  • I have following text in a Word document : "123". I created a style that has Font Hidden checked. I apply the style to '2' in that text. I want to hide this text 123 using add-in so I set

    MyRange.Font.Hidden = 1; 

    But then 1 and 3 is hidden and 2 is unhidden ? 2 is toggled. How do I hide it completely ? Note that I don't want to go character by character and inspect if it is hidden or not and hide it only if it is not hidden because I have a large document. 

    Thanks


    Saturday, December 31, 2016 3:14 PM

All replies

  • Instead of messing with individual characters or strings, apply the Style to the ranges then toggle the Style's Font.Hidden property. Do note, though, that formatting text as hidden is an unreliable way to trying to hide it. Whether it remains visible - or prints, regardless of whether it's visible - depends on how the user has Word configured. An action as simple as clicking on the ¶ character on the Ribbon's home tab will make visible all text formatted as hidden.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, December 31, 2016 7:38 PM
  • Hi MD2017,

    I made a test and I could reproduce your issue. In my option, this is Word built-in behavior.

    For a workaround, I would suggest you clear the format before setting Font.Hidden.

    Here is a simple code in VBA, you could implement in Add in.

        Selection.ClearFormatting
    
        With Selection.Font
            .Hidden = True
        End With

    Best Regards,

    Edward

       

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 2, 2017 6:02 AM
  • In my option, this is Word built-in behaviour.

    No, it isn't. The problem is that the code is faulty. True <> 1! True = -1. From the VBA help file:
    The True keyword has a value equal to -1.

    Try, for example:

    Sub Demo()
    Dim MyRange As Range
    Set MyRange = ActiveDocument.Paragraphs(2).Range.Words(4)
    MyRange.Font.Hidden = -1
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, January 2, 2017 7:37 AM
  • Yes, true it should be -1 but it doesn't solve the issue - I tried it also with -1 and it has the same behavior as in my original question.
    Thursday, January 5, 2017 2:19 PM
  • It works for me with the code I posted. Are you sure you actually have something in MyRange?

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, January 5, 2017 9:41 PM
    Thursday, January 5, 2017 9:40 PM
  • Hi Paul,

    I think you could reproduce this issue by following below steps.

    1. Enter "Test" in Word

    2. Create a new Style with Font.Hidden is True

    3. Format "e" in "Test" by this new Style

    4. Now Select "Test" and Set Font.Hidden as True,

    5. You will find "Tst" is Hidden and "e" is unhidden, I think op want to hide the whole of "Test".

    Hope you could find a solution for this issue. I tried my best, but I failed.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 6, 2017 6:07 AM
  • That's a rather tortured way of doing things and reflects nothing more than what I believe you'll find to be the documented behaviour for what can happen when you override a Style with direct formatting. It does not demonstrate that "Font.Hidden = -1 doesn't work as documented". That particular phenomenon has been known for decades.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 6, 2017 10:38 AM