The code will use whatever file is stored
in the XMLSourceFile property. If we want to write the data out to a new file, we pass in the
new file name. As in our original example, we??™re checking to ensure the map is exportable,
and then we??™re calling the ActiveWorkbook object??™s SaveAsXMLData method.
A Final Test
There is one last test to perform that will show just how using objects can compartmentalize
your code and provide easy-to-reuse functionality. In our HR workbook, we will create two
objects from the cXML class. Each will store its own mappings and property settings.
The following code shows the contents of the standard module containing the client
code for using the cXML class:
CHAPTER 3 n USING XML IN EXCEL 2007 117
Dim oEmpDept As cXML
Dim oHREmployees As cXML
'
Sub GetHREmployees()
Set oHREmployees = New cXML
oHREmployees.XMLSourceFile = ??
"C:\Chapter 3\HREmployees.xml"
Set oHREmployees.DataRange = Sheets(1).Range("A1")
oHREmployees.GetXMLData
End Sub
Public Sub GetEmpDept()
Set oEmpDept = New cXML
oEmpDept.XMLSourceFile = ??
"C:\Chapter 3\EmpDept.xml"
Set oEmpDept.DataRange = Sheets(2).Range("A1")
oEmpDept.GetXMLData
End Sub
Public Sub GetAdditionalEmpDeptInfo()
'appends data from files sent in from field offices.
If oEmpDept Is Nothing Then
Set oEmpDept = New cXML
End If
oEmpDept.XMLSourceFile = ??
"C:\Chapter 3\EmpDeptAdd.xml"
Set oEmpDept.DataRange = Sheets(2).Range("A1")
oEmpDept.GetXMLData False
End Sub
Public Sub AppendEmpDeptInfo()
'sample routine to get additional XML data w/o modifying XMLSourceFile Property
oEmpDept.
Pages:
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141