none
Hide entire ContentControl

    Question

  • Hi,

    I have a very simple requirement. I need to hide an entire contentcontrol in ms-word 2007 by using interop in a word-addin 2007 project (c# 4.0 and Visual Studio 2010).

    I am iterating all ContentControls in the document by using document.ContentControls. I want each ContentControl to be hidden. I am using the following code:

    contentControl.Range.Font.Hidden = 1;

    But this only affects the content of the ContentControl.

    In word I am able to do this by first selecting the entire contentControl (clicking on the grip) and set the font to hidden. Is there a possibility to do the same in interop?

    Regards,
    Koray


    F/\nt/\sy PRODUCTIONS

    Thursday, March 08, 2012 1:34 PM

All replies

  • i guess that .Range of content control is a place holder inside aforementioned control - you have to grab range that contains control or try to expand the one from control to one character to left and right.
    Thursday, March 08, 2012 8:52 PM
  • How?

    F/\nt/\sy PRODUCTIONS

    Friday, March 09, 2012 9:22 AM
  • Hi koraykazgan,

    Thanks for posting in the MSDN forum.

    That means the following snippet will work:

    Sub test()
        Dim Document As Document
        Dim item As ContentControl
        
        Set Document = Application.ActiveDocument
        For Each item In Document.ContentControls
            item.Parent.Range.Font.Hidden = True
        Next
    End Sub

    This is a VBA snippet. I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 13, 2012 6:56 AM
  • Hi Tom,

    thanks for your reply. My problem with your code snippet is that it will hide the entire document, because the parent of my contentcontrol is document (if inserted directly into the document). So it is not only hiding the contentcontrol, but also everything around it.

    Do I miss something?

    thanks,
    koray


    F/\nt/\sy PRODUCTIONS

    Wednesday, March 14, 2012 8:46 AM
  • Tom's code is very close, remove the parent should work.

    item.Range.Font.Hidden = True

    Does this do the trick for you?


    Forrest Guo | MSDN Community Support | Feedback to us

    Friday, March 16, 2012 8:17 AM
  • Hi Tom,

    your solution is not working for my requirement. Your code hides the content of the contentControl. Not the ContentControl itself. If you put your mouse cursor above the place where the contentControl sits, you will see, that ms-word is highlighting it.

    But i want to hide it entirely. In word, I select the entire contentControl by clicking its grip, and then set the font to hidden. Then the entire contentControl is hidden. And word does not highlight it anymore.

    So Range.Font.Hidden is not incorrect. But we need to do that on the contentControl. So I need to know how to select the ContentControl.

    Thanks,
    Koray


    F/\nt/\sy PRODUCTIONS

    Wednesday, March 21, 2012 9:55 AM
  • Hi Again,

    Our previous post is not up to date anymore. We have analized further and noticed the following:

    The code from Tom works, but the only problem is, that if we set the hidden property to True, we notice that it will be restored back to false in code (if we evaluate the value of the property right after the execution of the statement in debugger). 

    We are using the value of the hidden property to detemine if the contentControl is visible or not. So with the statement above it is not working, because it always say that the contentcontrol is visible.

    Check the following code sample:

        For Each cc In ContentControls
            If cc.ShowingPlaceholderText Then
                MsgBox "Is Hidden before set:" + CStr(cc.Range.Font.Hidden)
                cc.Range.Font.Hidden = True
                MsgBox "Is Hidden after set:" + CStr(cc.Range.Font.Hidden)
            End If
        Next

    Why is the value of hidden not returned correctly?

    Is there any other possibility to check if any contentControl is visible or not?

    Regards,
    Koray


    F/\nt/\sy PRODUCTIONS

    Wednesday, March 21, 2012 12:56 PM
  • Hi Koray,

    Here is a different version of your code, for a document with two content controls, that returns the current hidden property of the 'Selection.Font", but doesn't duplicate your experience through setting the font of the grip to hidden.

    For Each cc In ActiveDocument.ContentControls
        cc.Range.Select
        If Selection.Font.Hidden Then
        MsgBox "Is Hidden"
        Selection.Font.Hidden = False
        Else
        MsgBox "not hidden"
        Selection.Font.Hidden = True
        End If
    Next

    Repeat this several times to see the hidden property of each CC toggle back and forth.

    Do you need to keep the content controls if the end-user isn't going to see them or modify the text in them?  If so you can save the document as the base, open the same and rename it to "NoControls.docx" and iterate through the CCs, deleting each one. 

    CC.range
    range.select
    selection.delete

    After finishing the "NoControls.docx" either don't save or save, but you have the base document for future use - with visible contentcontrols.

    Regards,
    Chris Jensen
    Senior Technical Support Lead


    Chris Jensen

    Wednesday, March 21, 2012 7:31 PM
  • Hello Guys i use this code snippet from Tom:

    Sub test()   

    Dim Document As Document  

    Dim item As ContentControl  

    Set Document = Application.ActiveDocument 

    For Each item In Document.ContentControls   

    item.Range.Font.Hidden = True   

    Next

    End Sub

    We don't have the Problem with the hidden issue , so i'll prefer this one ...

    I only Need an query for a value in order to hide the Content control

    Does anyone know how  i can do this ?


    • Edited by john s, Tuesday, February 18, 2014 1:21 PM
    Tuesday, February 18, 2014 1:12 PM