How to create a custom content control linked to its copies with VBA?

93 views Asked by At

I would like to create a set of customized content controls in Word to be able to easily copy-paste them manually later on inside the same document. The trick is to get the same value in every copy by editing just one of them.

The code below imports custom xml nodes, inserts one control and maps it to corresponding node. I'm clueless why this copied control is not linked with it's original one. The value does not change after editing.

This is my code:

Option Explicit

Sub Add_Content_Controls()

    On Error GoTo Err_NoXMLPart
    Dim oCustomPart As Office.CustomXMLPart: Set oCustomPart = ActiveDocument.CustomXMLParts(4)

Err_ReEntry:
    Dim oCC As Word.ContentControl: Set oCC = ActiveDocument.ContentControls.Add
    With oCC
        .Type = wdContentControlRichText
        .Title = "Main_Doc_Number"
        .Tag = "Main"
        .SetPlaceholderText , , "Placeholder"
        .LockContentControl = True
        .Color = wdColorDarkRed
        .XMLMapping.SetMapping "/Document/Main/Main_Doc_Number[1]"
    End With
    Exit Sub

Err_NoXMLPart:
    ActiveDocument.CustomXMLParts.Add:  ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).Load ("D:\Downloads\DocumentXMLPart.txt")
    Resume Err_ReEntry
    
End Sub

xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<Document xmlns="Document">

    <Main>
        <Main_Doc_Number></Main_Doc_Number>
        <Main_Doc_Title></Main_Doc_Title>
    </Main>

    <Seconday>
        <Sec_Doc_Number></Sec_Doc_Number>
        <Sec_Doc_Title></Sec_Doc_Title>
    </Seconday>

</Document>
0

There are 0 answers