View Single Post
 
Old 08-12-2018, 10:49 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,365
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

Try:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim Tbl As Table, Rng As Range, i As Long
Dim oCCtrl As ContentControl
With CCtrl
  i = ActiveDocument.Range(0, .Range.Tables(1).Range.End).Tables.Count
  Select Case .Title
    Case "Spouse"
      If .Checked = True Then
        MsgBox "Adding spouse section"
        Set Rng = .Range.Tables(1).Range
        Call AddGroup(Rng, i)
      Else
        MsgBox "Removing spouse section"
        Set Rng = ActiveDocument.Tables(i + 1).Range
        Call RemoveGroup(Rng)
      End If
    Case "Child1"
      If .Checked = True Then
        MsgBox "Adding child section"
        Set Rng = .Range.Tables(1).Range
        Call AddGroup(Rng, i)
      Else
        MsgBox "Removing child section"
        Set Rng = ActiveDocument.Tables(i + 1).Range
        Call RemoveGroup(Rng)
      End If
    End Select
End With
End Sub

Sub AddGroup(Rng As Range, i As Long)
Dim CCtrl As ContentControl, t As Long, bAdd As Boolean
bAdd = False
If (i = ActiveDocument.Tables.Count) Then bAdd = True
If bAdd = False Then
  If ActiveDocument.Tables(i + 1).Rows.Count = 1 Then bAdd = True
End If
If bAdd = True Then
  With Rng
    .Collapse 0
    .Text = vbCr
    .Collapse 0
    .FormattedText = ActiveDocument.Tables(2).Range.FormattedText
    For Each CCtrl In .ContentControls
      With CCtrl
        t = .Type
        .Type = wdContentControlText
        .Range.Text = ""
        .Type = t
      End With
    Next
  End With
End If
End Sub

Sub RemoveGroup(Rng As Range)
Dim CCtrl As ContentControl
With Rng
  If .Tables(1).Rows.Count > 1 Then
    For Each CCtrl In .ContentControls
      CCtrl.LockContentControl = False
    Next
    .End = .End + 1
    .Delete
  End If
End With
End Sub
Note that I've employed a ContentControlOnExit macro - which makes more sense IMHO.

I'll leave it to you to figure out what to do for those with more than one spouse and/or child...

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote