none
BUG? ContentControl SetMapping.SetMappingByNode RRS feed

  • Question

  • There seems to be a bug in mapping a content control when the content control property "Content control cannot be deleted" property is set.  To see it:

    Sub ScratchMacro()
    Dim oCC As ContentControl
    Dim oCPart As CustomXMLPart
    Dim oNode As CustomXMLNode
    On Error Resume Next
    ActiveDocument.CustomXMLParts(4).Delete
    On Error GoTo 0
    Set oCPart = ActiveDocument.CustomXMLParts.Add("<Test>Help</Test>")
    Set oNode = oCPart.SelectSingleNode("/Test[1]")
    Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, Selection.Range)
    On Error GoTo Err_Handler
    'Set "Content control cannot be deleted property."  This causes a problem (bug??)
    oCC.LockContentControl = True
    oCC.XMLMapping.SetMappingByNode oNode
    Err_ReEntry:
    Exit Sub
    Err_Handler:
    MsgBox Err.Number & " " & Err.Description & vbCr + vbCr _
      & "Why? I am not trying to edit anything.  I am only trying to map the CC to a node."
    oCC.LockContentControl = False
    oCC.XMLMapping.SetMappingByNode oNode
    oCC.LockContentControl = True
    Resume Err_ReEntry
    End Sub
    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Saturday, July 2, 2011 2:51 AM

Answers

  • I agree, it sure looks like a bug. Once you know about it, though, the workaround in your code (unlock/map/lock) is easy enough, a lot like what we do for changing a protected legacy form.
    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Saturday, July 2, 2011 6:59 PM
  • The reason for what you're seeing is probably because, in the background, content controls are being destroyed and recreated "at the drop of a hat". (That's why the events have "in undo/redo", too.) So if the content control is protected against deletion, and changing the mapping (apparently) requires destroying and recreating the content control in the background, that can't be done.

    I suppose it then becomes a matter of opinion whether this is a bug or "by design". Certainly, it's not an elegant implementation in the context of the object model...


    Cindy Meister, VSTO/Word MVP
    Monday, July 4, 2011 8:46 AM
    Moderator

All replies

  • I agree, it sure looks like a bug. Once you know about it, though, the workaround in your code (unlock/map/lock) is easy enough, a lot like what we do for changing a protected legacy form.
    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Saturday, July 2, 2011 6:59 PM
  • The reason for what you're seeing is probably because, in the background, content controls are being destroyed and recreated "at the drop of a hat". (That's why the events have "in undo/redo", too.) So if the content control is protected against deletion, and changing the mapping (apparently) requires destroying and recreating the content control in the background, that can't be done.

    I suppose it then becomes a matter of opinion whether this is a bug or "by design". Certainly, it's not an elegant implementation in the context of the object model...


    Cindy Meister, VSTO/Word MVP
    Monday, July 4, 2011 8:46 AM
    Moderator
  • Jay/Cindy,

    Thanks for your interest and your posts.

     


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Tuesday, July 5, 2011 2:21 AM
  • Hi Greg,

    Have the issue been resolved? Can you confirm Cindy's conjecture?


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 3:13 AM
    Moderator