Ok, ok, I removed the bullet and wrote a nice VBA macro. I decided that I would share with you all if someone else runs into the same problem.
This macro basically calls the built-in XML Export () method of Excel, and then performs a series of text replacements in the resulting file. Text replacements are entirely up to you. Just place them on a sheet, as in the link below ...
Example of setting replacement rules: Click me for a screen
In this example, I replaced the tab with a space ": ns1" with an empty, "ns1:" with an empty and truncated root element with the original root element.
You can format your replacement rules in any way convenient for you if you follow these instructions:
- Select all the "find what" cells and give them the name * "FindWhat" (do not include the selection line in your selection, spaces will be ignored).
- Select all the cells "replace to" and give them the name * "ReplaceWith" (there should be a one-to-one mapping between the cells "find what" and "replace with", use spaces to remove unwanted text).
- Enter the name of the XML map somewhere in your book and name this cell "XmlMap".
- Run the macro. (You will be prompted to specify the file you want to export.)
* If you are not familiar with naming ranges in Excel 2007, go to the Formulas tab and select Name Manager.
OK, I wonβt keep you on your toes (LOL) ... here is the code for the macro. Just put it in the module in the VBA editor. I do not offer any guarantees with this free code (you can easily break it if you name the ranges incorrectly), but the examples I tried worked for me.
Option Explicit Sub ExportXml() Dim exportResult As XlXmlExportResult Dim exportPath As String Dim xmlMap As String Dim fileContents As String exportPath = RequestExportPath() If exportPath = "" Or exportPath = "False" Then Exit Sub xmlMap = range("XmlMap") exportResult = ActiveWorkbook.XmlMaps(xmlMap).Export(exportPath, True) If exportResult = xlXmlExportValidationFailed Then Beep Exit Sub End If fileContents = ReadInTextFile(exportPath) fileContents = ApplyReplaceRules(fileContents) WriteTextToFile exportPath, fileContents End Sub Function ApplyReplaceRules(fileContents As String) As String Dim replaceWorksheet As Worksheet Dim findWhatRange As range Dim replaceWithRange As range Dim findWhat As String Dim replaceWith As String Dim cell As Integer Set findWhatRange = range("FindWhat") Set replaceWithRange = range("ReplaceWith") For cell = 1 To findWhatRange.Cells.Count findWhat = findWhatRange.Cells(cell) If findWhat <> "" Then replaceWith = replaceWithRange.Cells(cell) fileContents = Replace(fileContents, findWhat, replaceWith) End If Next cell ApplyReplaceRules = fileContents End Function Function RequestExportPath() As String Dim messageBoxResult As VbMsgBoxResult Dim exportPath As String Dim message As String message = "The file already exists. Do you want to replace it?" Do While True exportPath = Application.GetSaveAsFilename("", "XML Files (*.xml),*.xml") If exportPath = "False" Then Exit Do If Not FileExists(exportPath) Then Exit Do messageBoxResult = MsgBox(message, vbYesNo, "File Exists") If messageBoxResult = vbYes Then Exit Do Loop RequestExportPath = exportPath End Function Function FileExists(path As String) As Boolean Dim fileSystemObject Set fileSystemObject = CreateObject("Scripting.FileSystemObject") FileExists = fileSystemObject.FileExists(path) End Function Function ReadInTextFile(path As String) As String Dim fileSystemObject Dim textStream Dim fileContents As String Dim line As String Set fileSystemObject = CreateObject("Scripting.FileSystemObject") Set textStream = fileSystemObject.OpenTextFile(path) fileContents = textStream.ReadAll textStream.Close ReadInTextFile = fileContents End Function Sub WriteTextToFile(path As String, fileContents As String) Dim fileSystemObject Dim textStream Set fileSystemObject = CreateObject("Scripting.FileSystemObject") Set textStream = fileSystemObject.CreateTextFile(path, True) textStream.Write fileContents textStream.Close End Sub
devuxer
source share