2

Backstory
I'm attempting to use VBA to delete several nodes from an Excel sheet XML file. The nodes correspond to filtering done on a sheet in the workbook, and I'm trying to remove the filtering.

The reason I'm removing the filtering is due to some corruption related to the filtering. I can open the file after Excel repairs it, but it opens the file in a "Repaired" mode which is cumbersome to save. The repairs dialog shows the following:

Removed Records: Sorting from /xl/worksheets/sheet2.xml part

Why it says 'sorting' I'm not sure. I'm positive the corruption is related to the filtering of the sheet. My thought then is to remove those nodes so that Excel doesn't have to repair the file...

What I've Tried
I've managed to open the XML file and delete the appropriate nodes. See the following code:

Public Sub DeleteXMLNodes()
    Dim oXMLDoc As MSXML2.DOMDocument
    Dim sFileName As String
    Dim xlFolder As String

    xlFolder = "C:\Users\Documents\ReadOnly Testing3\xl\worksheets\"
    sFileName = "Sheet2.xml"
    If Len(xlFolder) = 0 Then
        GetXMLFromFile = ""
    Else
        Set oXMLDoc = New MSXML2.DOMDocument
        oXMLDoc.Load xlFolder & sFileName

        Dim nodeList As IXMLDOMNodeList
        Dim node As MSXML2.IXMLDOMNode
        Dim deleteMe As IXMLDOMNode
        Dim oldChild As IXMLDOMNode
        Dim i As Long

        'Retrieve the node list by using an XPath query.
        'Set nodeList = oXMLDoc.SelectNodes("/worksheet/sheetData/row/c")
        Set nodeList = oXMLDoc.SelectNodes("/worksheet/autoFilter/filterColumn")

        For i = 1 To nodeList.Length
            Set deleteMe = oXMLDoc.SelectSingleNode( _ 
                           "/worksheet/autoFilter/filterColumn[" & i & "]")
            Set oldChild = deleteMe.ParentNode.RemoveChild(deleteMe)
        Next
        oXMLDoc.Save (xlFolder & sFileName)
        Set oXMLDoc = Nothing
    End If
End Sub

The Issue
The problem is that once I've deleted the nodes, zipped the files back up, and try to open them as an .xlsm file, I get a message that the file is corrupt and Excel is unable to repair it.

Is there another node or piece of data somewhere in the other XML files that needs to be deleted too? What is Excel finding that would flag the file as corrupt?

Before I saved the corrupt Excel file, I saved a good file to compare the corrupted file to. From what I can see they are the same after deleting the filter nodes...

Has anyone done this before and know what I'm missing?

1 Answer 1

0

You shold make sure to delete the parent part - of it is in fact being emptied. Check out this sample code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.