Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA中处理可选的XML属性?_Xml_Excel_Vba_Error Handling - Fatal编程技术网

如何在VBA中处理可选的XML属性?

如何在VBA中处理可选的XML属性?,xml,excel,vba,error-handling,Xml,Excel,Vba,Error Handling,我编写了一些代码,将一些数据从XML文件导入excel,直到它尝试读取不存在的属性为止;它们在文件中是可选的,我不能添加它们,所以我需要在代码中处理它们 我尝试过使用If Is Not Nothing处理对象,但这不起作用,如果没有运气,If”“或If Null,也不起作用 如果有人能给我任何帮助,我将不胜感激 Public Sub import() Dim oDoc As MSXML2.DOMDocument Dim fSuccess As Boolean Dim o

我编写了一些代码,将一些数据从XML文件导入excel,直到它尝试读取不存在的属性为止;它们在文件中是可选的,我不能添加它们,所以我需要在代码中处理它们

我尝试过使用
If Is Not Nothing
处理对象,但这不起作用,如果没有运气,
If”“
If Null
,也不起作用

如果有人能给我任何帮助,我将不胜感激

Public Sub import()

    Dim oDoc As MSXML2.DOMDocument
    Dim fSuccess As Boolean
    Dim oRoot As MSXML2.IXMLDOMNode
    Dim oSoftkey As MSXML2.IXMLDOMNode
    Dim oAttributes As MSXML2.IXMLDOMNamedNodeMap
    Dim oSoftkeyName As MSXML2.IXMLDOMNode
    Dim oSoftkeyDescriptor As MSXML2.IXMLDOMNode
    Dim oSoftkeyStyleName As MSXML2.IXMLDOMNode

    Dim oChildren As MSXML2.IXMLDOMNodeList
    Dim oChild As MSXML2.IXMLDOMNode
    Dim intI As Integer
    On Error GoTo HandleErr

    Set oDoc = New MSXML2.DOMDocument

    oDoc.async = False
    oDoc.validateOnParse = False
    fSuccess = oDoc.Load(ActiveWorkbook.Path & "\keys.xml")

    If Not fSuccess Then
      GoTo ExitHere
    End If

    intI = 2
    ActiveSheet.Cells(1, 1).CurrentRegion.ClearContents
    ActiveSheet.Cells(1, 1) = "Name"
    ActiveSheet.Cells(1, 2) = "TextDescriptor"
    ActiveSheet.Cells(1, 3) = "StyleName"

    ' Get the root of the XML tree.
    ' Set oRoot = oDoc.DocumentElement
    Set oRoot = oDoc.SelectSingleNode("//IMS_Softkeys")

    ' Each IMS_Softkey in IMS_Softkeys
    For Each oSoftkey In oRoot.ChildNodes

      Set oAttributes = oSoftkey.Attributes

      Set oSoftkeyName = oAttributes.getNamedItem("Name")
      Set oSoftkeyDescriptor = oAttributes.getNamedItem("TextDescriptor")
      Set oSoftkeyStyleName = oAttributes.getNamedItem("StyleName")

      ActiveSheet.Cells(intI, 1).Value = oSoftkeyName.Text

      'Can't handle optional attribute "TextDescriptor" or "SoftkeyStyle"
      ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
      ActiveSheet.Cells(intI, 3).Value = oSoftkeyStyleName.Text

      intI = intI + 1
    Next oSoftkey
ExitHere:
    Exit Sub
HandleErr:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume ExitHere
    Resume
End Sub
示例XML文件(keys.XML):



它们是对象,在VBA中,您可以使用以下语法检查它们是否为空(已签名)

如果不是(对象为空),则

因此,如果要检查属性是否是从XML检索和分配的,则可以:

' Print only if the `oSoftKeyDescriptor` is not nothing
If Not (oSoftkeyDescriptor Is Nothing) Then
    ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
End If

If Not (oSoftkeyStyleName Is Nothing) Then
    ActiveSheet.Cells(intI, 3).Value = oSoftkeyStyleName.Text
End If
我相信这就是你所追求的结果


非常感谢,我使用的是
如果没有,那也没什么问题。“现在很好用了。”sab0tage我很高兴能帮上忙。对于堆栈溢出,我们通过以下方式表示感谢。在您勾选的左侧答案旁边有一个绿色复选标记:)
' Print only if the `oSoftKeyDescriptor` is not nothing
If Not (oSoftkeyDescriptor Is Nothing) Then
    ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
End If

If Not (oSoftkeyStyleName Is Nothing) Then
    ActiveSheet.Cells(intI, 3).Value = oSoftkeyStyleName.Text
End If