Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-24-2022, 03:16 AM
Farming BA Farming BA is offline VBA content control to display/hide a section of text Windows XP VBA content control to display/hide a section of text Office 2016
Novice
VBA content control to display/hide a section of text
 
Join Date: Nov 2022
Posts: 3
Farming BA is on a distinguished road
Default 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)
Attached Files
File Type: docx Document for MS Forum.docx (58.1 KB, 22 views)
Reply With Quote
  #2  
Old 11-24-2022, 06:29 AM
macropod's Avatar
macropod macropod is offline VBA content control to display/hide a section of text Windows 10 VBA content control to display/hide a section of text Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
The code goes in the 'ThisDocument' code module of the document or its template.

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]
Reply With Quote
  #3  
Old 11-24-2022, 01:31 PM
Farming BA Farming BA is offline VBA content control to display/hide a section of text Windows XP VBA content control to display/hide a section of text Office 2016
Novice
VBA content control to display/hide a section of text
 
Join Date: Nov 2022
Posts: 3
Farming BA is on a distinguished road
Default

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
Attached Images
File Type: png Code.PNG (63.6 KB, 62 views)
Attached Files
File Type: docm Document for MS Forum latest.docm (65.3 KB, 19 views)
Reply With Quote
  #4  
Old 11-24-2022, 03:24 PM
macropod's Avatar
macropod macropod is offline VBA content control to display/hide a section of text Windows 10 VBA content control to display/hide a section of text Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Additionally, I can find no checkbox titled 'Checkbox2'.

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]
Reply With Quote
  #5  
Old 11-24-2022, 04:03 PM
Farming BA Farming BA is offline VBA content control to display/hide a section of text Windows XP VBA content control to display/hide a section of text Office 2016
Novice
VBA content control to display/hide a section of text
 
Join Date: Nov 2022
Posts: 3
Farming BA is on a distinguished road
Default

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
Attached Images
File Type: jpg VBA This Document Code.jpg (68.4 KB, 63 views)
File Type: png Checkboxes.PNG (14.8 KB, 60 views)
Attached Files
File Type: zip Virtual Basic Recording (1).zip (1.03 MB, 14 views)
Reply With Quote
  #6  
Old 11-25-2022, 12:20 AM
Italophile Italophile is offline VBA content control to display/hide a section of text Windows 11 VBA content control to display/hide a section of text Office 2021
Expert
 
Join Date: Mar 2022
Posts: 315
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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.
Reply With Quote
  #7  
Old 11-25-2022, 05:50 PM
macropod's Avatar
macropod macropod is offline VBA content control to display/hide a section of text Windows 10 VBA content control to display/hide a section of text Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See attached implementation using field coding and custom document properties.
Attached Files
File Type: docm Demo Form.docm (71.8 KB, 58 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA content control to display/hide a section of text How to Display Text on Multiple dropdown content control result philip.anthony Word VBA 11 03-28-2022 02:15 PM
VBA content control to display/hide a section of text 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
VBA content control to display/hide a section of text 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft