Private Sub WorkbookXmlImportEvents()
' Create a new DataTable.
Dim ds As New DataSet()
Dim dt As DataTable = ds.Tables.Add("Customers")
dt.Columns.Add(New DataColumn("LastName"))
dt.Columns.Add(New DataColumn("FirstName"))
' Add a new row to the DataTable.
Dim dr As DataRow = dt.NewRow()
dr("LastName") = "Chan"
dr("FirstName") = "Gareth"
dt.Rows.Add(dr)
' Add a new XML map to the collection.
Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add(ds.GetXmlSchema(), _
"NewDataSet")
' Import the data stream if the XmlMap was successfully created.
If Not (xmlMap1 Is Nothing) Then
' Get objects that can be used while the ExcelLocale1033
' attribute is true (the default).
Dim workbook1 As Excel.Workbook = TryCast(Microsoft.Office.Tools. _
Excel.ExcelLocale1033Proxy.Unwrap(Me.InnerObject), Excel.Workbook)
Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
range1 = TryCast(Microsoft.Office.Tools.Excel. _
ExcelLocale1033Proxy.Unwrap(range1), Excel.Range)
xmlMap1 = TryCast(Microsoft.Office.Tools.Excel. _
ExcelLocale1033Proxy.Unwrap(xmlMap1), Excel.XmlMap)
' This will raise the BeforeXmlImport and AfterXmlImport events.
workbook1.XmlImportXml(ds.GetXml(), xmlMap1, True, _
range1)
Else
MsgBox("The XmlMap could not be created")
End If
End Sub
Sub ThisWorkbook_BeforeXmlImport(ByVal Map As Excel.XmlMap, _
ByVal Url As String, ByVal IsRefresh As Boolean, _
ByRef Cancel As Boolean) Handles Me.BeforeXmlImport
If DialogResult.No = MessageBox.Show("Microsoft Excel is about" & _
" to import XML into the workbook. Continue with importing?", _
"Custom XML Import Dialog", MessageBoxButtons.YesNo) Then
Cancel = True
End If
End Sub
Sub ThisWorkbook_AfterXmlImport(ByVal Map As Excel.XmlMap, _
ByVal IsRefresh As Boolean, ByVal Result As Excel.XlXmlImportResult) _
Handles Me.AfterXmlImport
If Result = Excel.XlXmlImportResult.xlXmlImportSuccess Then
MsgBox("XML import succeeded.")
Else
MsgBox("XML import failed.")
End If
End Sub