PDA

View Full Version : [SOLVED:] Looping through content controls, then make criteria invisible.



MacroWizard
11-10-2015, 10:37 AM
New challenge guys,
:banghead:

I can't seem to make any of my controls invisible programmatically. I am using the following code, which works to identify the controls and successfully loops through the controls; however, I can't seem to find the ".visible" property. Any help?



Sub ParticipantGuide()
Dim ctl As ContentControl
For Each ctl In ActiveDocument.ContentControls
If ctl.Type = wdContentControlRichText Then
If ctl.Tag = "FacilitatorBlock" Then
ctl.Visible = False
Else
' DO NOTHING
End If
End If
Next
End Sub


I may be missing something, overlooking something. Hopefully someone here can help.

Thanks in advance for the creative notions.

p.s. I am not set on using these controls. If I need to change to something else, I can do that. Also, the controls are on the document itself, not on a form.

MacroWizard
11-10-2015, 11:37 AM
Alright so I've made some progress. I can embed the controls within a shape;
however, I need the shape to adjust its size as I type. Right now, the
richtextbox conforms to the shape. I need the shape to conform to the rest of
the document, otherwise users are going to have to resize the shape before they
type. That's just annoying.

Why there isn't a .visible property on word developer controls is beyond
me.

Any ideas on where to go from here, or suggestions as to what might work?

gmaxey
11-10-2015, 02:34 PM
There is no visible property for content controls. They were introduced nearly ten years ago. They were half baked then and remain half baked. Even more egregious in my opinion is no CC change event. Well there is no sense barking at the moon.

What are you really trying to achieve. You can make CC appear invisible (unless the use clicks where it is) by setting its placeholder text to a zero width space and its range to a null sting:

Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTag("FacilitatorBlock").Item(1)
oCC.SetPlaceholderText , , ChrW(8203)
oCC.Range.Text = vbNullString

If you want to toggle it between showing text and appearing to be invisible, you will probably have to map it to a CustomXMLPart and then toggle it between a node containing one state data and a different node containing a null string.

MacroWizard
11-10-2015, 08:43 PM
Greg,

I have to say that I am honored to have you reply to my post! I have been peeling over your website since last week and I am loving it. I actually just took a look at one of your templates "Gallery Control Demonstration" to figure out how to get that all set up and portable.

I am working on a special project that uses images that are stored within the building blocks portion of a template. The entire thing revolves around being able to press buttons to insert prefabbed items into the facilitator training document. This particular post is so that the facilitator parts can be "turned off" and the training developer can print the participant guide (everything but facilitator content).

Thank you for all of the reading resources. I'll keep that code nugget in my back pocket and implement it when I get to the office tomorrow morning.

gmaxey
11-10-2015, 09:39 PM
You will find some example code for mapping CC to toggle data here: http://gregmaxey.mvps.org/word_tip_pages/toggle_data_display.html
or shoot me a feedback and I can send you a sample document.

Boris_R
11-11-2015, 05:17 AM
Why there isn't a .visible property on word developer controls is beyond
me.
Any ideas on where to go from here, or suggestions as to what might work?
Try code

Sub ParticipantGuide()
Dim ctl As ContentControl
For Each ctl In ActiveDocument.ContentControls
If ctl.Type = wdContentControlRichText Then
If ctl.Tag = "FacilitatorBlock" Then
ctl.Range.Font.Hidden = True 'inserted instead "ctl.Visible = False"
Else
' DO NOTHING
End If
End If
Next
End Sub

gmaxey
11-11-2015, 08:07 AM
Boris,
Thanks for your post, but unfortunately that solution has very limited applicability. It will obviously work for you or you wouldn't have posted but if you sent your document an code to me it wouldn't because I typically have non-printing characters (including hidden text) showing. Additionally it has no effect on other content e.g. a picture in within the CC range.

MacroWizard
11-11-2015, 10:40 AM
Greg,

Thanks for your post. I would love a sample document as I am not exactly sure how to incorporate the slice of code you posted. Mine doesn't seem to want to work.

Boris,

Thanks for your post. I actually came across this method yesterday. It works great for text, but it isn't reliable for other content controls. Another method is to embed the content control within a shape and make the shape invisible. The only problem with that is that shapes tend to control the contents within them, versus making the shape conform to the rest of the document (text, tables, margins, etc). There is a macro workaround for making the shape behave properly, but the whole thing is just too clunky.