出于某种原因,我的代码正在丢弃此错误消息:运行时间错误91:对象变量或块变量未设置。
它突出显示代码:sheets(“ sheet1”)。单元格(i,4).value = mlfda.getElementsbytagname(“ status”)(0).Text
Sub PullComplexXML()
Dim xmlDoc As Object
Dim XMLPGA As Object
Dim xmlfda As Object
Dim i As Integer
'Dim xmlDoc As Object
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.Load ("C:\books.xml")
' Adjust "SectionName"
Dim sectionNode As Object
Set sectionNode = xmlDoc.SelectSingleNode("//PGAs")
If Not sectionNode Is Nothing Then
Dim childNode As Object
For Each childNode In sectionNode.ChildNodes
Debug.Print childNode.Text
Next childNode
Else
MsgBox "Section not found"
End If
'Pull nested data
' Create a new XML Document object
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
' Configure properties
xmlDoc.async = False
xmlDoc.validateOnParse = False
' Load the XML file
If Not xmlDoc.Load("C:\books.xml") Then
MsgBox "Failed to load XML file. Exiting."
Exit Sub
End If
i = 2
'Loop through each PGA
For Each XMLPGA In xmlDoc.DocumentElement.ChildNodes
'Loop through each FDA
For Each xmlfda In XMLPGA.ChildNodes
Sheets("Sheet1").Cells(i, 1).Value = xmlDoc.DocumentElement.getAttribute("Product") 'Product
Sheets("Sheet1").Cells(i, 2).Value = XMLPGA.getAttribute("PGAs") ' PGA
Sheets("Sheet1").Cells(i, 3).Value = xmlfda.getAttribute("FDA") ' FDA
Sheets("Sheet1").Cells(i, 4).Value = xmlfda.getElementsByTagName("Status")(0).Text ' Status
Sheets("Sheet1").Cells(i, 5).Value = xmlfda.getElementsByTagName("DisclaimReason")(0).Text ' DR
i = i + 1
Next xmlfda
Next XMLPGA
Set xmlDoc = Nothing
MsgBox "Data loaded. Now validate your file!"
End Sub