Mapping Content Controls to External XML

G

Greg Maxey

Posting here for lack of or not knowing a better place.

Last year I experimented with mapping content controls and had some success.
I learned that I could map contentcontrols to a customXMLPart and that that
part could be defined in an external source.

My experimenting XML file is pretty basic:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<myinfo xmlns="http://gregmaxey.mvps.org/CustomXML.htm">
<Name>Gregory K.Maxey</Name>
<DOB>12/31/1958</DOB>
<Gender>Male</Gender>
<Occupation>Self Employed</Occupation>
</myinfo>

I created a document and added a few ContentControls with the appropriate
titles and then ran this code:

Sub AddCustomXMLPartAndMapNamedCCs()
'Maps named CCs
Dim oCustPart As CustomXMLPart
Dim rngStory As Word.Range
Dim oCC As ContentControl
Dim oCCs As ContentControls
ClearXMLParts
Set oCustPart = ActiveDocument.CustomXMLParts.Add
oCustPart.Load ("F:\Data Stores\ExternalXML.xml")
For Each rngStory In ActiveDocument.StoryRanges
Do
Set oCCs = rngStory.ContentControls
For Each oCC In oCCs
Select Case oCC.Title
Case Is = "Name"
oCC.XMLMapping.SetMapping ("ns0:myinfo/ns0:Name")
Case Is = "DOB"
oCC.XMLMapping.SetMapping ("ns0:myinfo/ns0:DOB")
Case Is = "Gender"
oCC.XMLMapping.SetMapping ("ns0:myinfo/ns0:Gender")
Case Else
'Do nothing
End Select
Next oCC
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
Set rngStory = Nothing
Set oCC = Nothing
Set oCCs = Nothing
End Sub

Sure enough, the CCs were in fact mapped to the XML.

I realized that I could change the information in the XML and then open my
document, run the code again, and the CCs would update to the new data.

I am really a novice with XML. While I think what I have done is pretty
neat, it falls short of what I would like to do if a)it could be done, and
b)I knew how to do it.

I am trying to come up with a way to have a grouping of documents that share
common data and if that common data is changed in one then those changes
would be reflected in the others whenever one of those documents was
reopened or printed.

Once I run the code above the external becomes physically part of the
document data store and there is no link to the original file. I can change
the data in the ContentControl and the XML is the CustomXMLPart is changed
but the original file is not.

If there is a way to truly map a CC to an external XML file then if I
changed the data in the CC it would be reflected in the external XML file.
If I had several documents with CCs mapped to that external XML then they
would in effect be mapped to each other. Change data in a CC in one
document and that change would be reflected in other documents.

Is this possible?

Thanks.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
P

Peter Jamieson

Greg,

Nothing I have ever seen or found suggests that there is any way to
"connect" a content control to anything outside the .docx (i.e. the ZIP
file), either directly or indirectly via some kind of pointer in the data
store or some feature of xpath. If that is correct, then nothing you put in
a content control is ever going to be pushed back out to an external file or
data source of any kind (let's call it an external container) by Word. If
/that/ is correct, then in order to achieve the kind of sharing you
envisage, you would have to ship the data back to that external data source
programmatically. I can envisage trying to do that in various ways but have
not yet tried any of them myself so know nothing about feasibility, e.g.
a. use events to detect changes in either a content control or the data
store, get the new version of that piece of data, and ship it out to that
external container
b. use events to detect changes in either a content control or the data
store, get the new version of the entire xml document that contains that
piece of data and ship that out to the external container
c. wait until the user saves/closes the document then ship the data (and
you'd probably have to know which data) from the data store to your external
container, either using VBA or using the Open XML stuff that works directly
with the XML in the docx.

(c) is probably how things like Sharepoint do this kind of thing. I think if
it was easy there would already be loads of articles on the web about it.

As for ensuring the right data is there when you open or print, I think you
would have either to use appropriate events to get the current data from the
store or force people to open or print in a particular way (not my game, but
sometimes it's the only way).

I have to assume that the "one-way" nature of all this is deliberate - I
think Microsoft probably avoids having user-accessible features that can
easily affect/drag in stuff outside the document ever since the "scare"
about malicious software using fields to update other stuff and/or capture
data while the user wasn't looking. Or maybe it's because over the years
they have moved back from the idea that documents should be "active" things
with code in them to the idea that they are passive objects and you activate
them using traditional programming methods.

Although I suspect it might not be all that hard to do something "quick and
dirty" along the lines you suggest, it may be worth considering a few
traditional problem areas such as "do you need to make the 'external
container' multi-user?" e.g. what happens if code tries to update it and
it's locked (or gone), or people overwrite each other's updates with no
warning, etc. etc. You'd probably also need to know how much you need to
know about what's in the container, if you only ship out bits of it, and
whether that information is available from the data store itself, available
only if the xml part is associated with a schema inside word, or is defined
by your application.
 
G

Greg Maxey

Peter,

Thanks for taking the time to craft such an interesting reply. I am not
sure if I really understand all of it, but I understand enough to know that
whatever it would take is far above my current abilities. XML in general
simply shivers my timbers and perhaps I should leave it alone. The whole
idea was a child of my attempts last week to create bundled documents.
Where a user could enter common data in a data sheet then create a batch of
documents that share that data. Then later edit the data sheet and have
those edits reflected in the documents achieved reasonable success with
simple templates, formfields and bookmarks (happy to share with you if you
want to have a look). I was doing this for a person and rather than spend
more time trying to "perfect" it I recommend they look into one of the
commercial products that a fellow MVP offers. She took that recommendation
so I have let the project go.

Thanks again.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
P

Peter Jamieson

She took that recommendation so I have let the project go.

Smart move, IMO!
XML in general simply shivers my timbers and perhaps I should leave it
alone.

As with all such things, if you need or want to learn it, the thing is not
to try to grasp everything at once, and ideally have something easy to aim
for. The basic ideas in XML are pretty straightforward but there is a lot of
detail, and I think most people will find some parts of "the core XML
family" significantly harder to grasp than others.
 
C

Cindy M.

Hi Greg,

Mostly academic at this point, but...
Where a user could enter common data in a data sheet then create a batch of
documents that share that data. Then later edit the data sheet and have
those edits reflected in the documents achieved reasonable success
Best would probably be to use an Excel sheet and link in the data from there.

As Peter says, there's really nothing in the Office XML functionality that
is two-way. You can get this with a VSTO 2008 document-level customization.
That extends ContentControls to link with an outside data source. (Does the
work for you that Peter describes.)

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 :)
 

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

Top