Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-01-2022, 10:03 AM
jlyons jlyons is offline Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Windows 11 Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Office 2021
Novice
Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked
 
Join Date: Mar 2022
Posts: 2
jlyons is on a distinguished road
Unhappy Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked

I have a Word document with 11 yes or no questions, some of which have subquestions using ContentControl Checkboxes. The goal is to restrict editing except for ContentControls, and to send the document to clients. When I lock the document to send to a client, I want them to check the first answer yes or no, and if they answer no the subquestions are no longer relevant and I don't want them to be visible. I've found a way to hide the subquestions with a heading which is collapsed by default but when the document is locked moving through the ContentControls sequentially expands the headings automatically. I'm trying to write a macro to remove the subquestion Checkboxes if the answer to the main question is no. It would also be nice to expand the heading if the answer is yes because some of the questions don't have ContentControls in the subquestions. I have little/no experience with VBA and I'm sure it shows in my code, but any help would be much appreciated. Thanks!

Sub DeleteCheckboxes()

Dim aCC As ContentControls
Dim doc As Word.Document
Dim q1ifyes As Style
Dim q1nobox As ContentControl
Dim q1yesbox As ContentControl
Dim q1byes As ContentControl
Dim q1bno As ContentControl
Dim q1cyes As ContentControl
Dim q1cno As ContentControl

Set doc = ActiveDocument


Set q1yesbox = doc.SelectContentControlsByTag("q1_yesbox").Item(1 )
Set q1nobox = doc.SelectContentControlsByTag("q1_nobox").Item(1)
Set q1byes = doc.SelectContentControlsByTag("q1b_yesbox").Item( 1)
Set q1bno = doc.SelectContentControlsByTag("q1b_nobox").Item(1 )
Set q1cyes = doc.SelectContentControlsByTag("q1c_yesbox").Item( 1)
Set q1cno = doc.SelectContentControlsByTag("q1c_nobox").Item(1 )

q1yesbox.Type = wdContentControlCheckBox
q1byes.Type = wdContentControlCheckBox
q1bno.Type = wdContentControlCheckBox
q1cyes.Type = wdContentControlCheckBox
q1cno.Type = wdContentControlCheckBox

For Each q1nobox In ActiveDocument.ContentControls
If q1nobox.Checked Then
q1byes.Delete (True)
q1bno.Delete (True)
q1cyes.Delete (True)
q1cno.Delete (True)
End If
Next q1nobox
On Error Resume Next
End Sub
Reply With Quote
  #2  
Old 04-01-2022, 07:05 PM
Guessed's Avatar
Guessed Guessed is offline Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Windows 10 Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I wouldn't do this with collapsed subheadings. Instead I would place the subquestions into a rich text Content Control and then hide/show that range according to the answer they give. This code and attached document shows how I've made it so you don't need to edit the code to add more sections - just set up the CC title or tag properties in matching pairs.
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean)
  Dim aCCont As ContentControl
  If aCC.Tag = "Has Subquestions" Then
    For Each aCCont In ActiveDocument.SelectContentControlsByTitle(aCC.Title & " Subquestions")
      aCCont.Range.Font.Hidden = Not aCC.Checked
    Next aCCont
  End If
End Sub
Attached Files
File Type: docm Demonstration.docm (33.0 KB, 10 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-07-2022, 06:49 AM
jlyons jlyons is offline Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Windows 11 Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked Office 2021
Novice
Script to delete ContentControl Checkboxes by Tag if a specific Checkbox is checked
 
Join Date: Mar 2022
Posts: 2
jlyons is on a distinguished road
Default

This worked! Thank you so much!
Reply With Quote
Reply

Tags
checkboxes, contentcontrols



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide one bookmark when Two checkboxes are checked AVarg123 Word VBA 4 04-01-2022 06:21 PM
Add / Sum checked Checkboxes labelled 1, 2, 3, etc. in Word Table - Need Macro? klearazkrystal Word VBA 7 03-31-2019 11:50 PM
Need Macro to select Checkbox after two boxes are checked in a series of checkboxes rsaini Word VBA 0 01-16-2018 11:57 AM
Wanting to use checkboxes etc in a TOC to delete specific pages in a 300 page document Tammy Word 4 10-11-2016 05:53 PM
Checkboxes are unchecked in Listbox that were checked off before.How can I stop this? Rochelle711 Excel Programming 0 06-22-2014 06:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:24 AM.


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