如何解决Word 2016-如何在VBA中添加/删除重复节内容控件
尝试编写vba代码以添加和删除专门命名为RSCC的重复项部分。
Is it possible to add Repeating Section Content Control section with VBA?
解决方法
此宏将插入一个重复节CC:
Sub AddRepeatingSectionCC()
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRepeatingSection,Selection.Range)
With oCC
.AllowInsertDeleteSection = True
.RepeatingSectionItemTitle = "Repeating Section Item"
End With
Set oCC = Nothing
End Sub
这是设置其他CC选项的第二个宏:
Sub SetOptions()
If Selection.Information(wdInContentControl) Then
With Selection.ParentContentControl
'Sets the appearance to the original bounding box look. For the newer tags look,use wdContentControlTags
'If you don't need to change a setting,comment it out before running the macro
.Appearance = wdContentControlBoundingBox
'Sets the color of the control to a preset color
.Color = wdColorWhite
'Sets whether the Content Control can be deleted or not. If the control has had .Temporary = True applied,you must reverse that property to True before applying this.
.LockContentControl = True
'Sets whether the contents of the Content Control can be deleted or not.
.LockContents = False
'Sets the placeholder text or prompt for the control
.SetPlaceholderText,"Default Text"
'Sets the Content Control tag property
.Tag = "Tag"
'If this is set to true,the Content Control will be removed when the contents are edited.
.Temporary = False
'Sets the title of the Content Control. This appears on a tab above the control when it is activated.
.Title = "Title"
End With
Else
MsgBox "Please select a Content Control to change its options."
End If
End Sub
要删除它:
Sub DeleteCC()
If Selection.Information(wdInContentControl) Then
Selection.ParentContentControl.Delete
Else
MsgBox "Please select a Content Control to delete it."
End If
End Sub
,
要删除专门命名为RSCC的重复项部分,只需编辑在另一个问题中找到的代码即可。
Set cc = ActiveDocument.SelectContentControlsByTitle("RepCC").Item(1)
'to delete the first RepeatingSectionItem
cc.RepeatingSectionItems.Item(1).Delete
'to delete all but the first RepeatingSectionItem
Dim index As Long
For index = cc.RepeatingSectionItems.Count To 2 Step -1
cc.RepeatingSectionItems.Item(index).Delete
Next index
'to delete the entire ContentControl
cc.Delete
,
谢谢您的回应提摩太。我想我们快到了。我可能应该从以下内容开始:
对于Macro1,我下面的代码可以成功运行,但并非总是在每个后续部分之后或末尾插入,而是始终在第一部分之后添加。换句话说,如果我有5个重复部分(1,2,3,4,5),而不是在末尾插入(1,5,6),它会在第一个部分(1 ,6,5)然后(1,7,6,5)依此类推。目标是在末尾添加(1,7),依此类推。
Sub Macro1()
'
' Macro1 Macro
'
'
Dim cc As Word.ContentControl
Dim repCC As Word.RepeatingSectionItem
Set cc = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
Set repCC = cc.RepeatingSectionItems.Item(1)
repCC.InsertItemAfter
End Sub
对于Macro2,如果有五个部分(1、2、3、4、5),则下面建议的代码会同时删除除第2部分外的所有部分。目标是只删除最后一个部分,然后再删除。保护第一节不被删除(因为这是Macro1用于添加的“父”节)。
Sub Macro2()
'
' Macro2 Macro
Set cc = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
'to delete the first RepeatingSectionItem
cc.RepeatingSectionItems.Item(1).Delete
'to delete all but the first RepeatingSectionItem
Dim index As Long
For index = cc.RepeatingSectionItems.count To 2 Step -1
cc.RepeatingSectionItems.Item(index).Delete
Next index
'to delete the entire ContentControl
cc.Delete
'
End Sub
我下面的Macro2代码基于上面的Macro1代码,并且可以成功地删除一对一的重复节。问题在于它先删除第一个部分,而不是最后一个(1,7),然后再删除(2,7)然后是(3,5, 6,7),依此类推。目标是先删除最后一部分(1,6),然后再删除(1,5),然后再删除(1,4),依此类推,以防止第1部分被删除(因为这是Macro1用于添加的“父”部分)。
Sub Macro2()
'
' Macro2 Macro
'
'
Dim cc As Word.ContentControl
Dim repCC As Word.RepeatingSectionItem
Set cc = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
Set repCC = cc.RepeatingSectionItems.Item(1)
repCC.Delete
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。