Inserting custom XML into an open file: Can it be done? RRS feed

  • Question

  • I want to insert a custom XML part into an Open XML file. Problem: The file is currently open in Microsoft Excel 2007.


    Is there any way to do this?


    Using XML Mapping, I'm generating an XML file that I want to place inside the Open XML file after an event fires (i.e. the user clicks an export button). However, since the file is open, I don't think it will allow me to perform any functions using the Open XML object model. Creating a copy of the file, modifying it, and saving it doesn't seem like it would be ideal because then I'd have to reload the file in Excel. This would be very disruptive for the user.


    What's the division of labor between the Excel 2007 Object Model and the Open XML API? Can the two coexist at all?


    Or, do I have to always have to close whatever Office application I'm using before I do anything with the Open XML API? 

    Friday, August 10, 2007 3:55 PM


  • After doing some searching on MSDN I found the answer: Yes, it is possible.


    In VBA in Excel, I just ended up doing something like this:


    Code Snippet

    ' Exports XML Map and stores it in the Open XML package

    Sub Export_XML()
        Dim result As XlXmlExportResult
        Dim exportXML As String
        ' Get XML Map Export XML
        result = ActiveWorkbook.XmlMaps("Project_Map").exportXML(exportXML)
        If result = XlXmlExportResult.xlXmlExportSuccess Then
            ActiveWorkbook.CustomXMLParts.Add (exportXML)
            MsgBox ("Export failed!")
        End If
    End Sub




    Friday, August 10, 2007 6:17 PM