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.
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.