【发布时间】: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