Name

workbook.XmlImport(Url, ImportMap, [Overwrite], [Destination])

Synopsis

Imports an XML file into a list in the workbook. Returns an xlXMLImportResult value indicating whether the import succeeded.

Argument

Settings

Url

The address of the XML file to import. The file may be stored on the local machine or at a network address.

ImportMap

An XMLMap object from the workbook to use to interpret the XML. If omitted, Excel creates an XML map for the XML data.

Overwrite

True overwrites any data previously imported through the XML map; False appends data. Default is True.

Destination

A Range object identifying the upper-left corner of the destination for the imported data.

The ImportMap argument is required, but it doesn’t have to be initialized, since XmlImport creates the XML map if none exists. For example, the following code imports an XML file into a new worksheet and creates the XML map based on the XML source file:

Dim ws As Worksheet, xmap As XmlMap, msg As String
' Create a new worksheet for the imported data.
Set ws = ThisWorkbook.Worksheets.Add
ret = ThisWorkbook.XmlImport( _
  "http://www.mstrainingkits.com/excel/ExcelObjects.xml", _
  xmap, , ws.Range("A1"))
Select Case ret
    Case XlXmlImportResult.xlXmlImportElementsTruncated
        msg = "Data was truncated."
    Case XlXmlImportResult.xlXmlImportSuccess
        msg = "XML data imported successfully."
    Case XlXmlImportResult.xlXmlImportValidationFailed
        msg = "XML was not valid."
End Select
MsgBox msg

The return value of XmlImport ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.