Map CC to CustomDocumentProperty

Discussion in 'Word VBA' started by Greg Maxey, May 2, 2009.

  1. Greg Maxey

    Greg Maxey Guest

    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?


    --
    Greg Maxey - Word MVP

    My web site http://gregmaxey.mvps.org
     
    Greg Maxey, May 2, 2009
    #1
    1. Advertisements

  2. Greg Maxey

    Greg Maxey Guest

    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



    Greg Maxey wrote:
    > 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?


    --
    Greg Maxey - Word MVP

    My web site http://gregmaxey.mvps.org
     
    Greg Maxey, May 2, 2009
    #2
    1. Advertisements

  3. Greg Maxey

    Guest

    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 напиÑал:
    > 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.
     
    , Feb 6, 2014
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ranjit Kapila
    Replies:
    0
    Views:
    153
    Ranjit Kapila
    Jul 3, 2003
  2. Jane

    Document Map

    Jane, Sep 10, 2004, in forum: Word VBA
    Replies:
    1
    Views:
    97
    Word Heretic
    Sep 19, 2004
  3. Ward

    Showing the Document Map

    Ward, Feb 1, 2005, in forum: Word VBA
    Replies:
    3
    Views:
    100
    Word Heretic
    Feb 7, 2005
  4. Farmboy

    How do I change map on business card?

    Farmboy, Mar 3, 2005, in forum: Word VBA
    Replies:
    0
    Views:
    84
    Farmboy
    Mar 3, 2005
  5. Thanks_in_advance

    Map a Word document as HTML.

    Thanks_in_advance, Mar 25, 2005, in forum: Word VBA
    Replies:
    1
    Views:
    87
    Word Heretic
    Mar 30, 2005
  6. Dave B

    Document Map

    Dave B, Apr 12, 2005, in forum: Word VBA
    Replies:
    1
    Views:
    99
    Klaus Linke
    Apr 15, 2005
  7. cliffhayes
    Replies:
    4
    Views:
    113
    Tony Jollans
    Feb 26, 2006
  8. martin gifford
    Replies:
    0
    Views:
    190
    martin gifford
    Nov 9, 2009
Loading...