如何防止重复内容控件嵌套(word 2016)
How to prevent Repeat Content Control nesting (word 2016)
我在 word 2016 中构建的表单存在问题,除了 ContentControls 和 BuildingBlocks 的一般文档外,我无法真正从 Microsoft 找到任何内容。
这是一个简单的 Word 表单示例,我正在尝试使用添加和删除命令按钮以及名为 InspectorName
并标记为 InspectorName
:
的重复内容控件来创建
我使用以下 vba 代码将此内容控件作为 BuildingBlock 插入,同时突出显示段落标记前的内容控件
Public Sub getTemplateName()
Dim objTemp As Template
Dim myrange As Range
Dim myblock As BuildingBlock
Set objTemp = ActiveDocument.AttachedTemplate
Set myrange = Selection.Range
Set myblock = objTemp.BuildingBlockEntries.Add("InspectorName", _
wdTypeCustom1, "InspectorName", myrange)
End Sub
我现在正在尝试构建一个过程,如果存在内容控件(即计数不为 0),则将使用最后插入的内容控件的折叠范围插入使用上述过程构建的构建块.这是我下面的代码。
Public Sub insertBuildingBlock()
Dim objBB As BuildingBlock
Dim myrange As Range
Dim objTemp As Template
Dim mycount As Integer
Set objTemp = ActiveDocument.AttachedTemplate
Set objBB = objTemp.BuildingBlockTypes(wdTypeCustom1) _
.Categories("InspectorName").BuildingBlocks("InspectorName")
mycount = ActiveDocument.SelectContentControlsByTag("InspectorName").Count
If mycount <> 0 Then
Set myrange = ActiveDocument.SelectContentControlsByTag("InspectorName"). _
Item(mycount).Range
myrange.Collapse wdCollapseEnd
objBB.Insert myrange
End If
End Sub
此代码运行后会发生这种情况 - 两个内容控件是嵌套的。
我已经尝试了几乎所有我知道的方法,所以任何输入都将不胜感激!
当您折叠内容控件的范围时,范围的 "focus" 仍在内容控件内。添加此行:
myrange.MoveStart wdCharacter, 1
完整代码:
Public Sub insertBuildingBlock()
Dim objBB As BuildingBlock
Dim myrange As Range
Dim objTemp As Template
Dim mycount As Integer
Set objTemp = ActiveDocument.AttachedTemplate
Set objBB = objTemp.BuildingBlockTypes(wdTypeCustom1) _
.Categories("InspectorName").BuildingBlocks("InspectorName")
mycount = ActiveDocument.SelectContentControlsByTag("InspectorName").Count
If mycount <> 0 Then
Set myrange = ActiveDocument.SelectContentControlsByTag("InspectorName"). _
Item(mycount).Range
myrange.Collapse wdCollapseEnd
myrange.MoveStart wdCharacter, 1 '<-----
objBB.Insert myrange
End If
End Sub
我在 word 2016 中构建的表单存在问题,除了 ContentControls 和 BuildingBlocks 的一般文档外,我无法真正从 Microsoft 找到任何内容。
这是一个简单的 Word 表单示例,我正在尝试使用添加和删除命令按钮以及名为 InspectorName
并标记为 InspectorName
:
我使用以下 vba 代码将此内容控件作为 BuildingBlock 插入,同时突出显示段落标记前的内容控件
Public Sub getTemplateName()
Dim objTemp As Template
Dim myrange As Range
Dim myblock As BuildingBlock
Set objTemp = ActiveDocument.AttachedTemplate
Set myrange = Selection.Range
Set myblock = objTemp.BuildingBlockEntries.Add("InspectorName", _
wdTypeCustom1, "InspectorName", myrange)
End Sub
我现在正在尝试构建一个过程,如果存在内容控件(即计数不为 0),则将使用最后插入的内容控件的折叠范围插入使用上述过程构建的构建块.这是我下面的代码。
Public Sub insertBuildingBlock()
Dim objBB As BuildingBlock
Dim myrange As Range
Dim objTemp As Template
Dim mycount As Integer
Set objTemp = ActiveDocument.AttachedTemplate
Set objBB = objTemp.BuildingBlockTypes(wdTypeCustom1) _
.Categories("InspectorName").BuildingBlocks("InspectorName")
mycount = ActiveDocument.SelectContentControlsByTag("InspectorName").Count
If mycount <> 0 Then
Set myrange = ActiveDocument.SelectContentControlsByTag("InspectorName"). _
Item(mycount).Range
myrange.Collapse wdCollapseEnd
objBB.Insert myrange
End If
End Sub
此代码运行后会发生这种情况 - 两个内容控件是嵌套的。
我已经尝试了几乎所有我知道的方法,所以任何输入都将不胜感激!
当您折叠内容控件的范围时,范围的 "focus" 仍在内容控件内。添加此行:
myrange.MoveStart wdCharacter, 1
完整代码:
Public Sub insertBuildingBlock()
Dim objBB As BuildingBlock
Dim myrange As Range
Dim objTemp As Template
Dim mycount As Integer
Set objTemp = ActiveDocument.AttachedTemplate
Set objBB = objTemp.BuildingBlockTypes(wdTypeCustom1) _
.Categories("InspectorName").BuildingBlocks("InspectorName")
mycount = ActiveDocument.SelectContentControlsByTag("InspectorName").Count
If mycount <> 0 Then
Set myrange = ActiveDocument.SelectContentControlsByTag("InspectorName"). _
Item(mycount).Range
myrange.Collapse wdCollapseEnd
myrange.MoveStart wdCharacter, 1 '<-----
objBB.Insert myrange
End If
End Sub