VBA-Wenn <object> Nichts

Ich soll zum test ein Objekt zu sehen, wenn es nicht vorhanden ist. Wenn es nicht vorhanden ist, ich möchte einfach eine MsgBox auftauchen (oder Fehler beim schreiben in die Zelle A1 oder so). Banane existiert nicht in dieser XML.

<?xml version="1.0"?>
<catalog>
<book id="Adventure">
   <author>Gambardella, Matthew</author>
   <title>XML Developer's Guide</title>
   <price>44.95</price>
</book>
<book id="Adventure">
   <author>Ralls, Kim</author>
   <title>Midnight Rain</title>
   <price>5.95</price>
</book>
<book id="Adventure">
   <author>Boal, John</author>
   <title>Mist</title>
   <price>15.95</price>
</book>
<book id="Mystery">
   <author>Ralls, Kim</author>
   <title>Some Mystery Book</title>
   <price>9.95</price>
</book>
</catalog>

Den test-code:

Sub mySub()

Dim XMLFile As Variant
Dim Author As Object
Dim athr As String, BookType As String, Title As String, StoreLocation As String
Dim AuthorArray() As String, BookTypeArray() As String, TitleArray() As String, StoreLocationArray() As String
Dim i As Long, x As Long, j As Long, pn As Object, loc As Object, arr, ln As String, loc2 As Object

Dim mainWorkBook As Workbook
Dim n As IXMLDOMNode
Set mainWorkBook = ActiveWorkbook
Set XMLFile = CreateObject("Microsoft.XMLDOM")
XMLFile.Load ("C:\BooksOriginal.xml")

x = 1
j = 0


Set Author = XMLFile.SelectNodes("/catalog/book/banana")

If Author Is Nothing Then
    MsgBox ("Not Found")
    Range("A1").Value = "Not found"
End If

If Not Author Is Nothing Then
    For i = 0 To (Author.Length - 1)

    athr = Author(i).Text

    If athr = "Ralls, Kim" Then

        Set pn = Author(i).ParentNode
        BookType = pn.getAttribute("id")
        Title = pn.getElementsByTagName("title").Item(0).nodeTypedValue

        AddValue AuthorArray, athr
        AddValue BookTypeArray, BookType
        AddValue TitleArray, Title
        AddValue StoreLocationArray, StoreLocation

        j = j + 1
        x = x + 1
    End If
Next
    Range("A3").Resize(j, 1).Value = WorksheetFunction.Transpose(AuthorArray)
    Range("B3").Resize(j, 1).Value = WorksheetFunction.Transpose(BookTypeArray)
End If

End Sub

'Utility method - resize an array as needed, and add a new value
Sub AddValue(arr, v)
    Dim i As Long
    i = -1
    On Error Resume Next
    i = UBound(arr) + 1
    On Error GoTo 0
    If i = -1 Then i = 0
    ReDim Preserve arr(0 To i)
    arr(i) = v
End Sub

Warum wird in diesem block nichts tun? Ich fühle mich wie es ' s völlig übersehen zu VBA. Ich habe sogar versucht, indem Sie ein Ende in die If-Anweisung.

If Author Is Nothing Then
    MsgBox ("Not Found")
    Range("A1").Value = "Not found"
    End
End If

Auch, der Fehler wird auch ausgegeben, bei print range line.. das ist in der Wenn-Nicht Autor Ist Nichts Aussage. Sehr seltsam.

"Microsoft.XMLDOM" ist nicht der richtige Namensraum und Id instanziieren Sie ein DOM-Dokument. Die "Microsoft" - namespace gelöscht wurde vor vielen Jahren zugunsten der "MSXML2" und die meisten up-to-date-DOM-Dokument ist "DOMDocument.6.0". Für details auf dem MSXML2-version verwenden, finden Sie unter hier

InformationsquelleAutor NRH | 2015-04-21

Schreibe einen Kommentar