#1
|
|||
|
|||
Toggle Collapse/Expand via Content Control Checkbox
I've been trying for days to try to collapse/expand section headings via a content control check box and cannot get it working any assistance with the code would be extremely helpful. file attached.
|
#2
|
||||
|
||||
Try this code. It will run automatically when you exit the content control
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) If aCC.Type = wdContentControlCheckBox Then aCC.Range.Paragraphs(1).CollapsedState = Not aCC.Checked End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks Andrew for the quick reply, that code will certainly do the job; however I would need to swap it around unchecked vs checked. One other thing when I save, then reopen; it does not save the collapsed/expanded state.
|
#4
|
||||
|
||||
It is a 'feature' of Word to uncollapse the headings on open (so you don't get a surprise with content you didn't see).
To deal with that, you could have a macro which runs when you open the doc Code:
Private Sub Document_Open() Dim aCC As ContentControl For Each aCC In ActiveDocument.ContentControls If aCC.Type = wdContentControlCheckBox Then aCC.Range.Paragraphs(1).CollapsedState = aCC.Checked End If Next aCC End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
A little more difficult but eliminates the need to exit the checkbox. First retitle/tag the CCs Check_1 and Check_2 then map to a CustomXMLPart.
Code:
Option Explicit Dim WithEvents oCXPart As CustomXMLPart Sub AutoOpen() Dim aCC As ContentControl For Each aCC In ActiveDocument.ContentControls If aCC.Type = wdContentControlCheckBox Then aCC.Range.Paragraphs(1).CollapsedState = aCC.Checked End If Next aCC On Error Resume Next Set oCXPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://TheAnchorage.com/XMLPart").Item(1) lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterDelete(ByVal OldNode As Office.CustomXMLNode, ByVal OldParentNode As Office.CustomXMLNode, ByVal OldNextSibling As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when a #text node containing a value is replaced with Null value. Not applicable with checkboxes If OldParentNode.BaseName = "Number_of_children" Then ProcessChange OldNode, True lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterInsert(ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the Null value in a CXPart element node is replaced with a #text node containing a value. Not applicable with checkboxes. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the initial Null value in the node is replaced with a value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Sub ProcessChange(oNodePassed As Office.CustomXMLNode, Optional bDeadNode As Boolean = False) Dim oNode As CustomXMLNode Dim oCC As ContentControl If bDeadNode Then 'Not applicable for this case Else Set oCC = ActiveDocument.SelectContentControlsByTag(oNodePassed.ParentNode.BaseName).Item(1) oCC.Range.Paragraphs(1).CollapsedState = oCC.Checked End If lbl_Exit: Exit Sub End Sub |
#6
|
|||
|
|||
Wow some excellent coding ideas, thank you so much Andrew and Greg. I'm thinking of going your way Greg, I like the no exit checkbox. Thank you all for your time and expertise
|
Tags |
collapse, expand |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA Assign Checkbox Content Control Symbols | warbird | Word VBA | 3 | 01-16-2018 12:26 PM |
One Content Control Checkbox checks another Content Control Checkbox | DEsh | Word VBA | 2 | 10-06-2017 08:23 PM |
Content control checkbox calculations | allikat305 | Word Tables | 5 | 11-22-2016 11:20 PM |
Show Userform when Content Control CheckBox ticked | derajlance | Word VBA | 1 | 05-13-2016 01:55 PM |
Clicking the selected Content Control checkbox returns wrong control in vba event | DougsGraphics | Word VBA | 2 | 06-24-2015 07:31 AM |