Sub ImportXMLIntoExistingMap()
Dim sNewData As String
sNewData = "C:\projects\Excel\cds.xml"
ActiveWorkbook.XmlMaps("cds_Map").Import sNewData, True
End Sub
Saving XML Data
Saving your data back to the existing XML file or a new file is as simple as a call to the
Workbook object??™s SaveAsXMLData method. The SaveAsXMLData method takes two arguments,
the file name to save to and the XML map object to retrieve the file schema from. Figure 3-14
shows the SaveAsXMLData method displayed in the Visual Basic Immediate window with its
arguments shown via IntelliSense.
Figure 3-14. SaveAsXMLData method
Add a new procedure to the standard module you??™ve been working with, and name it
SaveXML.
Sub SaveXML()
Dim ExportMap As XmlMap
Set ExportMap = ActiveWorkbook.XmlMaps("cds_Map")
If ExportMap.IsExportable Then
ActiveWorkbook.SaveAsXMLData ??
"C:\projects\Excel\cds_XML_out.xml", ExportMap
Else
MsgBox ExportMap.Name & " cannot be used to export XML"
End If
End Sub
CHAPTER 3 n USING XML IN EXCEL 2007 107
Before persisting your data, it??™s a good idea to ensure that the XML is exportable. The
XMLMaps collection contains a read-only IsExportablemethod that returns True if any lists that
refer to the map are exportable. A map is not exportable if an invalid value for an element is
present or if required fields are not supplied.
Run the SaveXML macro from the Macros dialog box, accessible from the Developer ribbon.
Figure 3-15 shows the XML generated by the SaveXML subroutine.
Pages:
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129