#1
|
|||
|
|||
VBA content control to display/hide a section of text
Hi there,
I have a word document which I would like employees to complete when they want to initiate either an "investment" or a "project". It is a couple of pages long, which a number of sections to complete. 80% of the content required for each is the same, so I'm keen to have a single document. I want to place a dropdown list early on in the document, which prompts the user to indicate "investment" or "project". (A checkbox would also be suitable). Based on their selection, a section or text will either be hidden or displayed. I thought I had solved this using CTRL+F9 and using the {IF} conditional formula. However, when I got to testing, I realised it only worked in the Editing Restriction --> "Filling in Form" setting. This did not work, as the document is not "form-like"; as in, there are large areas of text that are just normal free-text, and inserting form fields everwhere would not work. I have attached the document I am working on. I have highlighted the sections I would like to hide/display based on the dropdown list selection. This starts on the second page. I have added some other annotations too (such as where the drop-down list should go). I would be very grateful if someone could point me in the right direction, before I spend further time on this. Many thanks in advance, Rick Farming BA (non-technical) |
#2
|
||||
|
||||
The following macro assumes you're using a Checkbox Content Control titled 'Checkbox1' and, for the text to be shown/hidden, a Rich Text Content Control titled 'ConditionalText':
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Title Case "Checkbox1" ActiveDocument.SelectContentControlsByTitle("ConditionalText")(1).Range.Font.Hidden = Not .Checked End Select End With End Sub With the checkbox checked, the content will be formatted as visible; unchecked, hidden. To reverse the options, delete the 'Not' from the code. Do note that whether text formatted as hidden will remain visible - or even be printed if not visible - depends on how the user has their Word installation configured.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you for your help, Macropod. I feel like I am almost there, but I require a little more guidance. This is all very new to me.
I have attached the latest version of the document. I have now added two Checklist Content Controls, titled "Checkbox1" and "Checkbox2". I have now also put my the sections of text which I would like to hide/display Rich Text Content Control boxes named "ConditionalText1" and "ConditionalText2". I have then entered the code you have provided me, in to the "ThisDocument" code module of the document (snip attached - as I'm not certain I'm in the right place). I made sure my naming matched the code. I did "Checkbox1" and "ConditionalText1" first. However, when I check and uncheck the box, nothing seems to happen. I would be grateful for further advice to help me over the line here, if possible. Rick |
#4
|
||||
|
||||
There is no code in your attachment. From your description you should have:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Title Case "Checkbox1" ActiveDocument.SelectContentControlsByTitle("ConditionalText1")(1).Range.Font.Hidden = Not .Checked Case "Checkbox2" ActiveDocument.SelectContentControlsByTitle("ConditionalText2")(1).Range.Font.Hidden = Not .Checked End Select End With End Sub Furthermore, I note your conditional texts include tables. Hiding the text in those content controls cannot hide the tables. Accordingly, you need a different approach - one that uses field coding to conditionally display the content or that conditionally inserts Custom Quick Parts into the document from its template.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Apologies, as you can tell I am very much a beginner. Appreciate you're help.
I have attached two snips: One shows the two checkboxes at the start of my document. These are titled in their properties: Checkbox1 and Checkbox2. I created these from the Developer Toolbar and selecting the Checkbox Control Button. I have then clicked into Visual Basic, and under the "ThisDocument" subfolder of "Normal", I have copied and pasted your code. I have added a snip of this. I have also added a short screen recording to show you what I am doing. Thanks, Rick |
#6
|
|||
|
|||
You’re putting the code in the wrong template. The code goes in the 'ThisDocument' code module of the document or its template, not Normal.
|
#7
|
||||
|
||||
See attached implementation using field coding and custom document properties.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to Display Text on Multiple dropdown content control result | philip.anthony | Word VBA | 11 | 03-28-2022 02:15 PM |
Content Control values NOT display name | silverspr | Word VBA | 9 | 09-30-2020 07:53 AM |
How to display tag in Content Control instead of title | vinbalraj | Word | 1 | 03-02-2018 08:55 AM |
How to manage (display/hide) parts of the text within a document via table of content | Curby | Word | 2 | 06-30-2016 12:29 AM |
Help with show/hide content control | acontreras | Word VBA | 0 | 10-21-2014 07:28 PM |