Share via

XML Mapping, Removing Parts

Anonymous
2020-12-31T18:00:09+00:00

I have a Word document that I am using XML and Custom Controls in.  I am using Word 365.  I have figured out how to add a Custom XML Part through the "Developer" tab and XML Mapping Pane and map a Custom Control to it.   I added a couple of variables and then reloaded the XML file.  Now I have two instances of the same XML part on the "Custom XML Part" drop down list.  How do you remove the old instance of the XML part from the "Custom XML Part" drop down box?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2021-01-02T14:27:41+00:00

    David,

      Microsoft's XML Mapping is basic at best.  Using their interface, you can’t remove items.  You can certainly do it with VBA.  Here I have a procedure that adds thee copies of an identical XML Part.  Then three methods to remove them:

    Sub AddIdenticalParts()

    'A basic Word macro coded by Greg Maxey

    Dim lngIndex As Long

      For lngIndex = 1 To 3

        ActiveDocument.CustomXMLParts.Add "<?xml version='1.0'?><CC_Map_Root xmlns='http://TheAnchorage.com'>" _

                                    & "<mapNode_1></mapNode_1><mapNode_2></mapNode_2><mapNode_3></mapNode_3><mapParentNode_1>" _

                                    & "<nestedMapNode_1></nestedMapNode_1><nestedMapNode_2></nestedMapNode_2>" _

                                    & "</mapParentNode_1></CC_Map_Root>"

      Next lngIndex

    lbl_Exit:

      Exit Sub

    End Sub

    Sub RemoveWithOutMercyAllNonBuiltInCXPsExceptFirst()

    Dim lngIndex As Long

      For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1

        ActiveDocument.CustomXMLParts(lngIndex).Delete

      Next lngIndex

    lbl_Exit:

      Exit Sub

    End Sub

    Sub RemoveDuplicatedNamespaces()

    Dim lngIndex As Long

    Dim oCXP As CustomXMLPart

    Dim oCol As New Collection

      For lngIndex = ActiveDocument.CustomXMLParts.Count To 3 Step -1

        Set oCXP = ActiveDocument.CustomXMLParts(lngIndex)

        On Error Resume Next

        oCol.Add oCXP.NamespaceURI, oCXP.NamespaceURI

        If Err.Number <> 0 Then

          oCXP.Delete

          Err.Clear

        End If

      Next lngIndex

    lbl_Exit:

      Exit Sub

    End Sub

    Sub RemoveDuplicatedTargetedNamespaces()

    Dim lngIndex As Long

    Dim oCXP As CustomXMLPart

    Dim oCol As New Collection

     Do

      On Error GoTo Err_Limit

      lngIndex = lngIndex + 1

      Set oCXP = ActiveDocument.CustomXMLParts.SelectByNamespace("http://TheAnchorage.com").Item(lngIndex)

      oCol.Add oCXP, CStr(lngIndex)

     Loop

    Process:

      For lngIndex = oCol.Count To 2 Step -1

        oCol.Item(lngIndex).Delete

      Next

    lbl_Exit:

      Exit Sub

    Err_Limit:

      Resume Process

    End Sub

    Or you could use the Advanced Mapping features of my CC Tools Add-In:

    https://gregmaxey.com/word\_tip\_pages/content\_control\_tools.html

    4 people found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-01-11T19:01:48+00:00

    Thank you for the information

    1 person found this answer helpful.
    0 comments No comments
  2. Charles Kenyon 166.6K Reputation points Volunteer Moderator
    2021-01-01T21:34:59+00:00

    I do not know of a way but there are others who respond in this forum who may. It is a holiday period in the U.S. Please be patient.

    Otherwise, you should be able to directly edit the XML.

    To do that, you change the filename extension from .docx to .zip. (I usually simply append the ".zip" to the filename after .docx.) Then open the file to examine the xml parts.

    If you want, you can save a copy of the problem document on OneDrive or DropBox and then post a view link here. Remove any confidential or proprietary information.

    Send Feedback to MS Developers

    1 person found this answer helpful.
    0 comments No comments