Word 2016-具有多个 RSCC,如何删除选择但防止第一部分在受保护的文档中被删除 (VBA)?
Word 2016- With multiple RSCCs, how to delete selection but prevent first section from being deleted in a protected document (VBA)?
我有一个包含多个 RepeatingSectionItems
的文档。以下代码用于在文档受保护时删除当前选择。但是,我正在尝试找出如何防止第一部分被选中时被删除,因为如果我删除了第一个重复部分,我将无法取回它,它会把一切都搞砸。
Application.ActiveDocument.Unprotect "green"
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
objCC.LockContents = False
objCC.AllowInsertDeleteSection = True
'
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
'loop through the repeatingsectionitems to find the one that selection is in
Dim rsi As RepeatingSectionItem
For Each rsi In CC.RepeatingSectionItems
If Selection.Range.InRange(rsi.Range) Then
rsi.Delete
Exit For
End If
Next rsi
End If
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
objCC.LockContents = True
objCC.AllowInsertDeleteSection = False
'
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:="green"
下面的代码遍历 RepeatingSectionItems
的集合,如果索引等于 1 则显示一个消息框,如果索引大于 1 则只删除该项目。
'loop through the repeatingsectionitems to find the one that selection is in
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 are attempting to delete the first item.", Title:="Unable to Delete Item"
End If
Exit For
End If
Next index
我有一个包含多个 RepeatingSectionItems
的文档。以下代码用于在文档受保护时删除当前选择。但是,我正在尝试找出如何防止第一部分被选中时被删除,因为如果我删除了第一个重复部分,我将无法取回它,它会把一切都搞砸。
Application.ActiveDocument.Unprotect "green"
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
objCC.LockContents = False
objCC.AllowInsertDeleteSection = True
'
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
'loop through the repeatingsectionitems to find the one that selection is in
Dim rsi As RepeatingSectionItem
For Each rsi In CC.RepeatingSectionItems
If Selection.Range.InRange(rsi.Range) Then
rsi.Delete
Exit For
End If
Next rsi
End If
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
objCC.LockContents = True
objCC.AllowInsertDeleteSection = False
'
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:="green"
下面的代码遍历 RepeatingSectionItems
的集合,如果索引等于 1 则显示一个消息框,如果索引大于 1 则只删除该项目。
'loop through the repeatingsectionitems to find the one that selection is in
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 are attempting to delete the first item.", Title:="Unable to Delete Item"
End If
Exit For
End If
Next index