小编Kse*_*ina的帖子

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

我有一个单词模板,内容控件包含在重复部分内容控件中.我需要创建一个按钮来删除项目转发器,例如添加.

我想弄清楚如何删除重复的部分项目.但在这种情况下 - 我总是删除最后一项.但是,我希望能够删除用户选择的项目.

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
Run Code Online (Sandbox Code Playgroud)

单词模板

我很乐意回答任何问题.

vba ms-word docx word-vba

5
推荐指数
1
解决办法
403
查看次数

标签 统计

docx ×1

ms-word ×1

vba ×1

word-vba ×1