1

I'm having an issue with a form I'm building in word 2016 and can't really find anything from Microsoft except the general documentation for ContentControls and BuildingBlocks.

Here is an example of a simple Word form I'm trying to create with add and remove command buttons and a repeating content control named InspectorName and tagged InspectorName:

enter image description here

I inserted this content control as a BuildingBlock using the following vba code whilst highlighting the content control before the paragraph mark

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

I am now trying to build a procedure where if there exists a content control (i.e. the count is not 0) then the building block built with the above procedure will be inserted using the collapsed range of the last content control inserted. Here is the code I have below.

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

After this code runs this occurs - the two content controls are nested.

enter image description here

I have tried almost everything I know, so any input would greatly be appreciated !

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Could you please explain what's *in* this content control buidling block? It appears to be two or three ActiveX controls? And is the bulding block of type "Repeating"? You write that in the title, but I don't see any evidence of it in the screenshots.. – Cindy Meister Jul 13 '18 at 10:39
  • Ah, and is your document in Design mode when the code runs (as shown in the pictures)? – Cindy Meister Jul 13 '18 at 11:26

1 Answers1

0

When you collapse a content control's Range the "focus" of the range is still within the content control. Add this line:

  myrange.MoveStart wdCharacter, 1

Full code:

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
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Thanks cindy will definitely do this. I actually kind of gave up and basically had a rich text content control parent from which I inserted all the repeating content controls. –  Jul 13 '18 at 15:13
  • Since you're relatively new to Stack Overflow “@Moro-Code: If a contribution answers your question you should click the check mark at its left. This lets others with a similar problem know what worked. And people who look for unanswered questions in order to help will realize it's been answered. Once you have enough rep points, you can upvote any contribution (comment or answer) on the site that you consider useful and downvote anything you find "not good". In order to "ping" someone put a @ in front of their name to be sure they get a message. – Cindy Meister Jul 16 '18 at 18:27
  • Hey @Cindy. Thank you for taking the time to explain this! –  Jul 16 '18 at 19:42