【问题标题】:Indexing of repeating content control items重复内容控制项的索引
【发布时间】:2019-03-18 06:00:13
【问题描述】:

我的问题是基于this 问题和this 解决方案:

我有类似的问题,但我需要按顺序插入项目,但我无法正确索引插入的重复内容控件。我不知道我应该提前插入多少项目,所以插入可以是完全动态的。 有人能帮帮我吗?

这是一个简单的代码:

Dim wordApp As Variant
Dim wDoc As Variant

Set wordApp = CreateObject("word.application")
wordApp.DisplayAlerts = False
Set wDoc = wordApp.Documents.Open(ThisWorkbook.Path & "/example.docm")
wordApp.Visible = True


Dim i As Integer
Dim counter As Integer
counter = 1

Dim cc As Variant
Dim repCC As Variant
Set cc = wDoc.SelectContentControlsByTag("container").Item(1)


For i = 1 To 4
        If counter <> 1 Then
            Set repCC = cc.RepeatingSectionItems.Item(cc.RepeatingSectionItems.Count)
            repCC.InsertItemAfter
        End If

        wDoc.SelectContentControlsByTag("number").Item(counter).Range.Text = counter
        counter = counter + 1

Next i

我的word doc的图片:

重复内容控件的标签名称为“容器”。富文本内容控件的标签名称为“数字”。

错误结果的图片:

还有我想得到的:)

提前感谢您的帮助!

【问题讨论】:

  • 所以您是说每次插入新的重复内容控件时,number 的值都应该动态变化?您是否尝试过为编号序列插入 SEQ 字段?这些不会自动更新,它需要手动更新域代码(选择,然后按 F9)或运行宏来执行此操作。但编号本身将是 SEQuential。
  • 感谢@CindyMeister 的评论和帮助!该任务与计数无关(这只是一个简化的示例)。我的问题是在插入新的重复内容控件时,我无法以正确的顺序填写其内部内容控件。内部内容控件的每个标签都是相同的,所以我认为它们是通过使用 .Item(1, 2, ...) 方法的索引来标识的。我想当插入一个新的重复内容控件时,它在前一个之下,并且其内部内容控件的索引增加了一个。但有些不对劲。

标签: excel vba ms-word


【解决方案1】:

终于可以解决我的问题了:

任务:这是从 vba 动态插入重复部分内容控件 (RSCC) 并按顺序填写其内部内容控件的简单示例。

问题:当插入像here这样的新RSCC时,它们的内容控件将获得相同的标签(标题),并且索引是随机分配的。

解决方案:当刚刚插入新的 RSCC 时,必须即时填写内容控件。

Dim cc As Variant
Dim repCC As Variant
Dim i As Integer

Set cc = wDoc.SelectContentControlsByTag("container").Item(1)

For i = 1 To 4 'it could be any number
        If i = 1 Then 'because already has a RSCC in the doc file, so I need only 3 more.
            Set repCC = cc.RepeatingSectionItems.Item(1)
        Else
            repCC.InsertItemAfter
            Set repCC = cc.RepeatingSectionItems.Item(i) '(or .Item(cc.RepeatingSectionItems.Count))
        End If

        For Each cc_current In repCC.Range.ContentControls
            Select Case cc_current.Tag
                Case Is = "number"
                    cc_current.Range.Text = i
                'Case Is = .....
            End Select
        Next cc_current
Next i

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多