Capturing a Content Control Range

G

Greg Maxey

Has anyone figured out a good way to capture a Content Control's Range? I
don't mean just the range of its text, but the text and it both.

I was working on some code to create a CC bound to CustomXML and then create
a BuildingBlock QuickPart of that control. I could never figure out a
simple way to define the range for the buidling block. If I simply used
..ContentControl(Index).Range the BB was just the text. Here is what I
finally put together but hoping one of you has a better way. Basically I
wrapped a bookmark around the Content control and then set the BB range to
the bookmark range:

Sub CreateBoundCC()
Dim oCustPart As CustomXMLPart
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Dim oCC As ContentControl
Dim oBB As BuildingBlock
Dim oTemplate As Template
Set oBMs = ActiveDocument.Bookmarks
Set oCustPart = ActiveDocument.CustomXMLParts.Add
oCustPart.Load ("E:\Data Stores\mysample.xml")
Set oRng = Selection.Range
oRng.End = oRng.End + 1
oBMs.Add "Temp", oRng
Set oRng = oBMs("Temp").Range
oRng.Collapse wdCollapseStart
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.Title = "Clien Name"
.XMLMapping.SetMapping "/root/A", , oCustPart
End With
Set oTemplate = ActiveDocument.AttachedTemplate
oBMs("Temp").Range.End = oBMs("Temp").Range.End - 1
Set oRng = oBMs("Temp").Range
oBMs("Temp").Delete
Set oBB = oTemplate.BuildingBlockEntries.Add("Client Name",
wdTypeCustomQuickParts, "My Custom BBs", oRng)
Set oCustPart = Nothing
Set oBB = Nothing
Set oCC = Nothing
Set oBMs = Nothing
Set oRng = Nothing
End Sub

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

User Form Issues 2
Bookmark question 4
Content Control text formatting 3
Clear button for text box 1
User Form Problems 4
A CheckBox Content Control 0
Clear Button 5
Using Bookmarks to Define a Range 3

Top