Friday, 25 March 2016

XML : Need to parse XML into Excel spreadsheet

My VB is rusty and I'm trying to parse an XML file into a spreadsheet without much success. There's a lot of fragmented documentation on accomplishing the task online but I don't have recent enough experience to bring it all together.

I want VB to prompt me to select an XML file when I open my spreadsheet and then sort through it to find the values. My sample XML file is here if its helpful.

The code I'm attempting (without success) is:

  Private Sub Workbook_Open()        Dim xmlFile As String      Dim xmlDoc As New DOMDocument      Dim book As Workbook      Set book = ActiveWorkbook        Dim fd As Office.FileDialog      Set fd = Application.FileDialog(msoFileDialogFilePicker)            With fd              .AllowMultiSelect = False              .Title = "Select the Circle42 report file."              .Filters.Clear              .Filters.Add "XML Files", "*.xml"                If .Show = True Then                  xmlFile = .SelectedItems(1)              End If            End With        xmlDoc.async = False        If xmlDoc.Load(xmlFile) = False Then      MsgBox ("XML LOAD ERROR")            Else               book.Sheets("Sheet1").Range("B7").Value = xmlDoc.SelectSingleNode("//Report/SAX3DFunction/LV/EDV").Attributes.getNamedItem("val").Text        End If    End Sub    

Thank you in advance. I don't understand what I'm doing wrong but I'm trying to get the value of //Report/SAX3DFunction/LV/EDV "val" (or 26.596 in this case) to the specified cell.

No comments:

Post a Comment