Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-07-2020, 09:39 PM
metaflowdesigns metaflowdesigns is offline Toggle Collapse/Expand via Content Control Checkbox Windows 7 64bit Toggle Collapse/Expand via Content Control Checkbox Office 2016
Novice
Toggle Collapse/Expand via Content Control Checkbox
 
Join Date: Feb 2020
Posts: 3
metaflowdesigns is on a distinguished road
Default 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.
Attached Files
File Type: docm Toggle expand.docm (21.4 KB, 21 views)
Reply With Quote
  #2  
Old 07-07-2020, 11:06 PM
Guessed's Avatar
Guessed Guessed is offline Toggle Collapse/Expand via Content Control Checkbox Windows 10 Toggle Collapse/Expand via Content Control Checkbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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

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
Reply With Quote
  #3  
Old 07-07-2020, 11:20 PM
metaflowdesigns metaflowdesigns is offline Toggle Collapse/Expand via Content Control Checkbox Windows 7 64bit Toggle Collapse/Expand via Content Control Checkbox Office 2016
Novice
Toggle Collapse/Expand via Content Control Checkbox
 
Join Date: Feb 2020
Posts: 3
metaflowdesigns is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 07-08-2020, 12:17 AM
Guessed's Avatar
Guessed Guessed is offline Toggle Collapse/Expand via Content Control Checkbox Windows 10 Toggle Collapse/Expand via Content Control Checkbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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

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
Reply With Quote
  #5  
Old 07-08-2020, 04:39 AM
gmaxey gmaxey is offline Toggle Collapse/Expand via Content Control Checkbox Windows 10 Toggle Collapse/Expand via Content Control Checkbox Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
Attached Files
File Type: docm Toggle Headings with Checkboxes.docm (27.3 KB, 37 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 07-08-2020, 02:53 PM
metaflowdesigns metaflowdesigns is offline Toggle Collapse/Expand via Content Control Checkbox Windows 7 64bit Toggle Collapse/Expand via Content Control Checkbox Office 2016
Novice
Toggle Collapse/Expand via Content Control Checkbox
 
Join Date: Feb 2020
Posts: 3
metaflowdesigns is on a distinguished road
Default

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
Reply With Quote
Reply

Tags
collapse, expand



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
Toggle Collapse/Expand via Content Control Checkbox One Content Control Checkbox checks another Content Control Checkbox DEsh Word VBA 2 10-06-2017 08:23 PM
Toggle Collapse/Expand via Content Control Checkbox Content control checkbox calculations allikat305 Word Tables 5 11-22-2016 11:20 PM
Toggle Collapse/Expand via Content Control Checkbox Show Userform when Content Control CheckBox ticked derajlance Word VBA 1 05-13-2016 01:55 PM
Toggle Collapse/Expand via Content Control Checkbox Clicking the selected Content Control checkbox returns wrong control in vba event DougsGraphics Word VBA 2 06-24-2015 07:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:51 AM.


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