XmlImport URL:= ??
"C:\Chapter 3\files\cds.xml", ImportMap:= ??
Nothing, Overwrite:=True, Destination:=Range("$A$1")
End Sub
nNote The path to the XML file will vary based on where you are storing the files that came with this book.
4. In the GetXMLData subroutine, change the name of the XML file we??™re opening to
cd.xml. The subroutine should look like Listing 7-8 now.
Listing 7-8. GetXMLData Procedure After Modification
Sub GetXMLData()
ActiveWorkbook.XmlImport URL:= ??
"C:\Chapter 3\files\cd.xml", ImportMap:= ??
Nothing, Overwrite:=True, Destination:=Range("$A$1")
End Sub
5. In Excel, makeWorksheet1 the active sheet.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 276
6. From the Macro dialog box, run the GetXMLData procedure. We get a very ugly error, as
shown in Figure 7-38.
Figure 7-38. A non-intuitive error message is presented to the user.
7. Click the End button.
In cases like this, where it??™s an issue of resource availability, we don??™t need to code an
actual error handler to assist the user. What we need to do is check for the existence of the file
before we try to open it. If the file is not present, we??™ll tell the user in a friendlier and more
understandable manner.
In the VBE, add a standard code module. Add the code from Listing 7-9.
Listing 7-9. The FileExists Function
Function FileExists(FilePathName As String) As Boolean
Dim blnReturn As Boolean
blnReturn = Len(Dir(FilePathName)) > 0
FileExists = blnReturn
End Function
On Standard Module1, modify the GetXMLData subroutine as in Listing 7-10.
Pages:
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261