Search code examples
xmlvbaexcelmsxml

Remove XML header using VBA


I have the xml file below;

<?xml version="1.0" encoding="UTF-8"?>
<response><XXX><id>1</id></XXX></response>

I want to remove the XML header <?xml version="1.0" encoding="UTF-8"?> from the file such that the xml file will look like this;

<response><XXX><id>1</id></XXX></response>

How can this be done using Excel VBA?

The purpose of doing this is so that I can load the header-less string into MSXML2.DOMDocument object through LoadXML() function.

Thank you.


Solution

  • You can use the Replace function...

    Dim DoubleQuote : DoubleQuote = Chr(34)
    Dim strHeader : strHeader = "<?xml version=" & DoubleQuote & "1.0" & DoubleQuote & " encoding=" & DoubleQuote & "UTF-8" & DoubleQuote & "?>"
    Result = replace(strHeader,strHeader,"")
    If Result = "" then
      MsgBox "Header stripped!"
    Else
      MsgBox "Failed to strip header."
    End If
    

    Hope that helps!