Word- VBA- 如果只剩下一个节,如何防止删除选定的重复节内容控件?
Word- VBA- How To Prevent Deletion of Selected Repeating Section Content Controls If There Is Only One Section Remaining?
以下代码成功删除了选定的 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 节或保护第 3 节,我想保护第 2 节如果第 1 节和第 2 节被删除。
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
以下代码成功删除了选定的 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 节或保护第 3 节,我想保护第 2 节如果第 1 节和第 2 节被删除。
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