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?
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 Xu [MSFT]
MSDN Community Support | Feedback to us
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?
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
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.
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?
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
If Selection.Font.Hidden Then
MsgBox "Is Hidden"
Selection.Font.Hidden = False
MsgBox "not hidden"
Selection.Font.Hidden = True
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.
After finishing the "NoControls.docx" either don't save or save, but you have the base document for future use - with visible contentcontrols.
Senior Technical Support Lead
- Proposed as answer by cjatmsModerator Wednesday, March 28, 2012 6:53 PM
Hello Guys i use this code snippet from Tom:
Dim Document As Document
Dim item As ContentControl
Set Document = Application.ActiveDocument
For Each item In Document.ContentControls
item.Range.Font.Hidden = True
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