【问题标题】:Word- VBA- How To Prevent Deletion of Selected Repeating Section Content Controls If There Is Only One Section Remaining?Word-VBA-如果只剩下一个部分,如何防止删除选定的重复部分内容控件?
【发布时间】:2021-03-29 09:09:00
【问题描述】:

以下代码成功删除了选定的 RSCC,但始终阻止删除第一个 RSCC。

 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

我的目标是能够删除任何选定的 RSCC,但如果还剩下任何一个 RSCC,则不能。

换句话说,如果我有三个 RSCC (1,2,3),而不是总是保护第 1 节,如果我要删除第 1 节和第 3 节,我想保护第 2 节,或者如果第 3 节,我想保护第 3 节1和2被删除了。

【问题讨论】:

  • 我看到了你以前的thread;请告诉我们您要做什么;您是否意识到删除内容控制将保留输入的文本,并且您可能允许也可能不允许用户删除内容控制?
  • 这个If index > 1 Then到底是什么?您正在使用For index = 1 To cc.RepeatingSectionItems.Count 进行迭代,因此在第二次迭代时,如果cc.RepeatingSectionItems.Count > 1,索引将始终> 1。
  • 这里显示的逻辑错误以及您之前帖子中的其他错误表明您没有理解一些基本概念;我建议您从更基础的程序流技术开始,并使用像 this 这样的技术
  • 我试图让用户删除任何选择,但始终保留一个部分。 If index > 1 Then 防止第一部分被删除。 If index > 2 Then 防止前两个部分被删除。目标是使该索引动态化。无论删除哪些部分,如果只剩下一个部分,用户将永远无法删除任何部分。
  • @MarceloScofanoDiniz - 发布的代码中没有逻辑错误。 For index = 1...Next 循环用于确定所选重复节项的索引号,因为在对象模型中没有这样做的方法。

标签: vba ms-word word-contentcontrol rowdeleting


【解决方案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

【讨论】:

  • 谢谢先生!圣诞快乐!
  • 直到现在才选择这个,但是这个代码可以防止最后一个部分被删除。查看代码,我看不出这是怎么可能的。只有在剩余任何部分时才应防止删除。
  • @MohamadBachrouche - 我的时间很宝贵。我不会再浪费时间向你解释这个了。
  • 好的。那么你能指出我可以帮助解释这一点的资源方向吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 2014-10-30
  • 2019-05-21
  • 1970-01-01
相关资源
最近更新 更多