Map CC to CustomDocumentProperty

G

Greg Maxey

I am trying to map a contentcontrol to the content of a
CustomDocumentProperty. With some help found here I have managed to create
a CustomXMLPart that contains the XML found in the docProps\custom.xml
folder. What I can't seem to get worked out is how to map to that data.
The following code will successfully map to a "BuiltInDocumentProperty" and
gives indications that it maps to a "CustomDocumentProperty." However, the
CC does not reflect the data in the CustomDocumentProperty after being
mapped.

Sub MapCCToCustomDocProperty()
Dim objcc As ContentControl
Dim objNode As CustomXMLNode
Dim blnMap As Boolean
Set objcc = ActiveDocument.ContentControls.Add(wdContentControlText,
Selection.Range)
'This works
'Set objNode = ActiveDocument.CustomXMLParts.SelectByNamespace _
'
("http://schemas.openxmlformats.org/package/2006/metadata/core-properties")
_
' (1).DocumentElement.ChildNodes(1)
'But this doesn't
Set objNode =
ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/"
_
&
"officeDocument/2006/custom-properties")(1).DocumentElement.ChildNodes(1)
Debug.Print objNode.XML
blnMap = objcc.XMLMapping.SetMappingByNode(objNode)
If blnMap Then
MsgBox "Eureka"
Else
MsgBox "Unable to map the content control."
End If
End Sub

This is the result of the Debug.Print objNode.XML:

<property
xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2"
name="Client"><vt:lpwstr
xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">Smithg</vt:lpwstr></property>

From all indications that I see, it seems that my CC should show "Smithg"
after being mapped. What am I missing?
 
G

Greg Maxey

I figured out the immediate proplem. There is another child node involved
with the data stored in docProcs\custom.xml. This works:

Sub MapCCToCustomDocProperty()
Dim objcc As ContentControl
Dim objNode As CustomXMLNode
Dim blnMap As Boolean
Set objcc = ActiveDocument.ContentControls.Add(wdContentControlText,
Selection.Range)
Set objNode =
ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/"
_
&
"officeDocument/2006/custom-properties")(1).DocumentElement.ChildNodes(4).ChildNodes(1)
blnMap = objcc.XMLMapping.SetMappingByNode(objNode)
If Not blnMap Then
MsgBox "Unable to map the content control."
End If
End Sub
 
D

dmi3.1415

Hi Greg. Greetings from Russia.
I tried to use your code with Word 2010 (vba), but the error appears:
"Run-time error '9':
Subscript out of range".
This error occurs in that string:
"Set objNode = ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/officeDocument/2006/custom-properties")(1).DocumentElement.ChildNodes(4).ChildNodes(1)".

Did I understand correctly that the data from the file 'docProps\custom.xml', I must put into a folder 'customXml'?
Or it is not necessary, and I am missing something else?
(The file 'docProps\custom.xml' the file is not empty and contains some 'properties')

PS Many thanks for http://gregmaxey.mvps.org/word_tips.html


Ñуббота, 2 Ð¼Ð°Ñ 2009 г., 9:02:51 UTC+5 пользователь Greg Maxey напиÑал:
 

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

Similar Threads

Actual Word VBA Help 15

Top