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/ns0OB")
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 !!!
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/ns0OB")
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 !!!