Delete Attributes RRS feed

  • Question

  • Posting here on the suggestion in another group to post in a more XML oriented group. Hope I'm in or at least closer to the right place.

    I am attempting, perhaps fruitlessly, to develop a rudimentary custom change event for a Word rich text content control. I am not a true programmer and just dabble with VBA.  However, I have working custom change event which detects changes in "text" and was hoping to expand it to detect any change.

    My thought was to set a string variable = to the WordOpenXML value in the control and then monitor the WordOpenXML and trigger a change event when it changes from the reference string value.  I learned right away that the value of the WordOpenXML is huge and contains attributes (rsid values) that can change when the XML is querried.

    What I have done since is to narrow the XML string down to a much smaller chunk.  What I am now trying to do is remove any attribute that has a basename of "rsidRPr" I found on a google search a method  .removeAttribute, but I can't make it work.

    For this code to work you need a blank Word document with a single richtext control containing a small bit of text.


    Sub ScratchMacro()
    Dim oXDoc As New MSXML2.DOMDocument
    Dim oNode As MSXML2.IXMLDOMNode
    Dim oNode2 As MSXML2.IXMLDOMNode
    Dim oNode3 As MSXML2.IXMLDOMNode
    Dim oAttr As MSXML2.IXMLDOMAttribute
    oXDoc.LoadXML (ActiveDocument.ContentControls(1).Range.WordOpenXML)
    oXDoc.setProperty "SelectionLanguage", "XPath"
    oXDoc.setProperty "SelectionNamespaces", _
      "xmlns:pkg=''" & _
    " xmlns:w=''"
    'Set a = oXDoc.SelectNodes("/pkg:package[1]/pkg:part[3]/pkg:xmlData[1]/w:document[1]/w:body[1]/w:p[1]")
    Set oNode = oXDoc.SelectSingleNode("/pkg:package[1]/pkg:part[3]/pkg:xmlData[1]/w:document[1]/w:body[1]/w:p[1]")
    For Each oNode2 In oNode.ChildNodes
      For Each oNode3 In oNode2.ChildNodes
        For Each oAttr In oNode2.Attributes
          If oAttr.BaseName = "rsidRPr" Then
            oNode2.removeAttribute (oAttr.BaseName)
          End If
      Next oNode3
    Next oNode2
    ActiveDocument.Range.InsertAfter vbCr & oNode.XML
    End Sub

    Greg Maxey Please visit my website at:

    Tuesday, November 11, 2014 3:36 PM


All replies