Word-VBA-如果只剩下一个节,如何防止删除选定的重复节内容控件



以下代码成功删除了选定的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,或者如果区段1和2被删除,则保护区段3。

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

最新更新