View Single Post
 
Old 04-01-2022, 10:03 AM
jlyons jlyons is offline Windows 11 Office 2021
Novice
 
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