gpt4 book ai didi

vba - 通过 docx 文件宏中的索引以编程方式删除重复节项?

转载 作者:行者123 更新时间:2023-12-03 01:52:33 27 4
gpt4 key购买 nike

我有一个 Word 模板,其中的内容控件包含在重复部分内容控件内。我需要创建一个按钮来删除项目重复器,例如添加。

我正在尝试找出如何删除重复部分的项目。但在这种情况下 - 我总是删除最后一项。但是,我希望能够删除用户选择的项目。

Sub delete()
Dim cc As ContentControl
Dim Index
Set cc = ThisDocument.SelectContentControlsByTag("ResolRepeater").Item(1)
With cc
.LockContentControl = False
.LockContents = False
.AllowInsertDeleteSection = True

For Index = 1 To cc.RepeatingSectionItems.Count
If Selection.Range.InRange(cc.RepeatingSectionItems(Index).Range) Or cc.RepeatingSectionItems(Index).Range.InRange(Selection.Range) Then
Exit For
End If
Next Index

'can't delete, get Run-Time Error '5904': "you can not change the range"
cc.RepeatingSectionItems(Index).Range.delete

'this lines always delete last element:
'cc.RepeatingSectionItems(Index).Range.Select
'Selection.Delete

End With
End Sub

word template

我很高兴得到任何答案..

最佳答案

您可以通过多种方式执行此操作,其中之一是处理 ContentControlBeforeDelete 事件 MSDN Link

或者,您可以使用当前的 Selection.range,检查该范围(包括开始和结束)是否具有 wdContentControlRepeatingSection 类型的内容控件,然后只需删除该控件即可。

类似于(代码未测试):

var contentControl = Selection.Range.ContentControls;
if (contentControl.Type == Microsoft.Office.Interop.Word.WdContentControlType)
{
contentControl.Delete();
}

关于vba - 通过 docx 文件宏中的索引以编程方式删除重复节项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39029771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com