Re: Mapping Content Controls to External XML
- From: "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 15 Oct 2008 16:33:53 -0400
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 *** then create a batch of
documents that share that data. Then later edit the data *** 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
Word MVP web site http://word.mvps.org
McCain/Palin '08 !!!
"Peter Jamieson" <pjj@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ObWvsBwLJHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
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.
--
Peter Jamieson
http://tips.pjmsn.me.uk
"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23LoCtLnKJHA.456@xxxxxxxxxxxxxxxxxxxxxxx
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
Word MVP web site http://word.mvps.org
McCain/Palin '08 !!!
.
- Follow-Ups:
- Re: Mapping Content Controls to External XML
- From: Cindy M .
- Re: Mapping Content Controls to External XML
- From: Peter Jamieson
- Re: Mapping Content Controls to External XML
- References:
- Mapping Content Controls to External XML
- From: Greg Maxey
- Re: Mapping Content Controls to External XML
- From: Peter Jamieson
- Mapping Content Controls to External XML
- Prev by Date: WINWORD.EXE process deos not end after closing the user form?
- Next by Date: Re: Embed pulldown menu in a document
- Previous by thread: Re: Mapping Content Controls to External XML
- Next by thread: Re: Mapping Content Controls to External XML
- Index(es):