Hi Rich,
I have a template that I want to create. In the template there is a content
control that is where someone would enter in a client name. There are
multiple places in the document that need the same client name. I want to
find a way to make the client name propogate through the document using the
least amount of code possible.
The way the designers of content controls set this up to be done is:
1. Create an XML file that reflects the data to be entered in the content
controls of the document. It does not have to reflect all the content controls
you use, but it should contain those that
- whose contents should be duplicated
- for which you want to read/write the data from an outside program
(without necessarily opening the Word application)
2. Add the XML file to the template as a CustomXMLPart. This can be done by
manually editing the dotx/dotm file zip package, or by using the object model
(with code). You need to do this only once, so you could use a VBA macro in
Normal.dot, for example.
3. The code then needs to "map" the XML node(s) in the XML to the corresponding
content controls in the template.
Once this link is established, anything typed in one control linked to a
particular XML node in the CustomXMLNode will automatically display in any
other content control linked to that same node. No macro code is required, once
the CustomXMLPart has been inserted and linked to the content controls (IOW you
only need it to set up the template, but not for use of the template).
There are any number of ways to code this. Below my signature is a set of
sample code I set up when someone asked about using more than one namespace.
That is totally irrelevant to your scenario, so don't worry about there being
two different sets of XML in the code sample. Just concentrate on how the (very
simple) XML is put into a CustomXMLPart, then linked to existing content
controls. What is important, however, is that you do need to specify a
Namespace in the XML, and that you need the Namespace in order to map the XML
nodes to the content controls.
FWIW, I don't think the approach suggested by Jie Wang will work the way you
need...
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail
Sub CreateCustomXMLPart()
Dim doc As Word.Document
Dim cp1 As Office.CustomXMLPart
Dim cp2 As Office.CustomXMLPart
Dim Xml1 As String, Xml2 As String
Xml1 = "<?xml version='1.0' encoding='utf-8'?><tree>oak</tree>"
Set doc = ActiveDocument
Set cp1 = doc.CustomXMLParts.Add(Xml1) 'To add XML from string
doc.Variables("Xml1") = cp1.ID 'Store for future reference
Set cp2 = doc.CustomXMLParts.Add 'To add XML from file on disk
cp2.Load "C:\Test\Flower.xml"
doc.Variables("Xml2") = cp2.ID
End Sub
Sub LinkContentControlsToXML()
Dim doc As Word.Document
Dim cc1 As Word.Contentcontrols
Dim cc2 As Word.Contentcontrols
Dim cc As Word.ContentControl
Set doc = ActiveDocument
Set cc1 = doc.SelectContentControlsByTitle("Name1")
For Each cc In cc1
cc.XMLMapping.SetMapping "/tree", , doc.CustomXMLParts.SelectByID
(doc.Variables("Xml1"))
Next
Set cc2 = doc.SelectContentControlsByTitle("Name2")
For Each cc In cc2
cc.XMLMapping.SetMapping "/flower", , doc.CustomXMLParts.SelectByID
(doc.Variables("Xml2"))
Next
End Sub