0

The following code successfully deletes the selected RSCC but always prevents the first RSCC from being deleted.

 Dim cc As ContentControl
    If Selection.Information(wdInContentControl) Then
        Set cc = Selection.ParentContentControl
        If Not cc.Type = wdContentControlRepeatingSection Then
            Do Until cc.Type = wdContentControlRepeatingSection
                Set cc = cc.ParentContentControl
            Loop
        End If
        Dim index As Long
        For index = 1 To cc.RepeatingSectionItems.Count
            If Selection.Range.InRange(cc.RepeatingSectionItems(index).Range) Then
                If index > 1 Then
                    cc.RepeatingSectionItems(index).Delete
                Else
                    MsgBox Prompt:="You cannot delete this.", Title:="Error"
                End If
                Exit For
            End If
        Next index
    End If

My goal is to be able to delete any selected RSCC but not if there is any one RSCC remaining.

In other words, if I have three RSCCs (1,2,3), instead of always protecting section 1, I would like to protect section 2 if I were to delete the section 1 and 3 or protect section 3 if section 1 and 2 were deleted.

  • I saw your previous [thread](https://stackoverflow.com/questions/64448786/word-2016-how-to-add-delete-repeating-section-content-control-in-vba); please, tell us what you are trying to do; do you realize that removing content control will preserve the entered text and you may or may not allow users to remove content control? – Marcelo Scofano Diniz Dec 18 '20 at 19:20
  • And what is this `If index > 1 Then` exactly? You are iterating with `For index = 1 To cc.RepeatingSectionItems.Count`, so at second iteration index will be > 1 always, if `cc.RepeatingSectionItems.Count > 1`. – Marcelo Scofano Diniz Dec 18 '20 at 19:25
  • The logic error shown here and other errors in your previous post suggest that you are not understanding some basic concepts; I suggest that you start with more fundamental program flow techniques and grab one like [this](https://www.amazon.com/Mastering-VBA-Microsoft-Office-365/dp/1119579333) – Marcelo Scofano Diniz Dec 18 '20 at 19:38
  • I am trying to allow the user to delete any selection but always preserve one section regardless. ```If index > 1 Then``` prevents the first section from being deleted. ```If index > 2 Then``` prevents the first two sections from being deleted. Goal is to make that index dynamic. No matter what sections are deleted, the user should never be able to delete any section if there is only one left. – Mohamad Bachrouche Dec 18 '20 at 21:00
  • @MarceloScofanoDiniz - there isn't a logic error in the posted code. The `For index = 1...Next` loop is to determine the index number of the selected Repeating Section Item as there isn't a method of doing that in the object model. – Timothy Rylatt Dec 19 '20 at 11:39
  • @Timothy: Indeed, I expressed myself wrong; I meant collection deletion from First to Last, instead of the usual backwards. Mohamad: if you want to preserve always one section, but not the first, as your code does, one way to achieve is, knowing which index you want to preserve, deleting backwards, skipping the one index that you want to preserve. – Marcelo Scofano Diniz Dec 19 '20 at 19:32
  • **following code successfully deletes the selected RSCC but always prevents the first RSCC from being deleted.** deletes the selected RSCC or all but first? – Marcelo Scofano Diniz Dec 19 '20 at 19:33
  • @MarceloScofanoDiniz - just the selected RSI. – Timothy Rylatt Dec 19 '20 at 20:08
  • @MarceloScofanoDiniz- Indexes will vary depending on what sections are added and deleted. I want to preserve at least one index regardless so is it a matter of using the ```count``` function so that when the RSCC only has one remaining section, the error runs to prevent the deletion of that single remaining section? – Mohamad Bachrouche Dec 20 '20 at 21:25
  • Your code is already preventing the `index = 1` to be deleted, right? But you want to provided a way to preserve, say, the `index = 2` deleting 1 and 3. Correct? – Marcelo Scofano Diniz Dec 21 '20 at 02:04
  • First things first: how do you know the index, ID, Tag or whatever marker from the RSCC that the user wants to preserve? This info is the key, because you need to compare it against the iterator and only preserve when `whatever = selected.whatever` – Marcelo Scofano Diniz Dec 21 '20 at 02:06
  • @MarceloScofanoDiniz - you're getting hung up on irrelevant detail. All the OP wants to do is ensure that `cc.RepeatingSectionItems.Count > 1` before deleting, and that `cc.RepeatingSectionItems.Count >= 1` after deleting. – Timothy Rylatt Dec 21 '20 at 09:15
  • @Timothy Rylatt- So do I not need to use ```index``` anymore? – Mohamad Bachrouche Dec 21 '20 at 20:02
  • You still need to use `index` as it is required to loop through the collection of RSI’s. – Timothy Rylatt Dec 21 '20 at 20:11
  • @TimothyRylatt, I still don't get it: his code already provides that one last section will remain; what his code does not assure is which is the correct index to preserve. As far as I know, [RSCC does not expose index to VBA](https://gregmaxey.com/word_tip_pages/content_controls.html). Knowing the correct index to preserve solve it because instead of `If index > 1 Then` he will do `If index <> indexToPreserve` – Marcelo Scofano Diniz Dec 21 '20 at 20:56
  • @MarceloScofanoDiniz - you're overthinking it. The index is irrelevant. The OP doesn't know which 'index' needs to be preserved. The required code simply needs to delete ***selected*** RSI's whilst always ensuring that the RSCC contains a minimum of one RSI. – Timothy Rylatt Dec 21 '20 at 21:19
  • @TimothyRylatt and Mohamad: take a look on [this thread](https://social.msdn.microsoft.com/Forums/lync/en-US/edebb7c9-5864-4d1f-ae06-b0dde8c40e7e/how-to-get-a-value-from-content-control-within-repeating-section-content-control?forum=worddev). I'm very inclined to suggest that although pretty for the user, RSCC it is really not the better way if you need to extract info or manipulate them through VBA. – Marcelo Scofano Diniz Dec 22 '20 at 02:05
  • @MarceloScofanoDiniz - I don’t know why you’re tagging me. It’s not my question. – Timothy Rylatt Dec 22 '20 at 03:17
  • Ok. Be safe, wear masks, happy 2021 :) – Marcelo Scofano Diniz Dec 22 '20 at 12:49
  • @TimothyRylatt- For starters, I tried changing ```For index = 1 To cc.RepeatingSectionItems.Count``` to ```For index = 0 To cc.RepeatingSectionItems.Count > 1``` and that didn't work or is it not that simple? – Mohamad Bachrouche Dec 22 '20 at 18:31

1 Answers1

1
   Dim cc As ContentControl
   If Selection.Information(wdInContentControl) Then
      Set cc = Selection.ParentContentControl
      If Not cc.Type = wdContentControlRepeatingSection Then
         Do Until cc.Type = wdContentControlRepeatingSection
            Set cc = cc.ParentContentControl
         Loop
      End If
      If cc.RepeatingSectionItems.count > 1 Then
         Dim index As Long
         Dim count As Long
         count = cc.RepeatingSectionItems.count
         For index = cc.RepeatingSectionItems.count To 1 Step -1
            If Selection.Range.InRange(cc.RepeatingSectionItems(index).Range) Then
               If count > 1 Then
                  cc.RepeatingSectionItems(index).Delete
                  count = count - 1
               Else
                  MsgBox Prompt:="There is only 1 item left so you cannot delete it.", Title:="Error"
               End If
            End If
         Next index
      Else
         MsgBox Prompt:="There is only 1 item left so you cannot delete it.", Title:="Error"
      End If
   End If
Timothy Rylatt
  • 7,221
  • 2
  • 10
  • 14