当我添加“for循环”(使用VBA检索XML)时,为什么我的代码突然停止工作

时间:2017-06-22 22:04:59

标签: xml vba loops for-loop

此代码有效:

Dim node
Set node = oXMLFile.SelectNodes("root/options/option_element[0]/value/text()")
For j = 0 To node.Length - 1
    .Content.InsertAfter node(j).NodeValue
 Next j
.Content.InsertParagraphAfter
Set node = Nothing

Set node = oXMLFile.SelectNodes("root/options/option_element[1]/value/text()")
For j = 0 To node.Length - 1
    .Content.InsertAfter node(j).NodeValue
 Next j
.Content.InsertParagraphAfter
Set node = Nothing

但是当我添加for循环时,此代码不起作用 - 为什么? (没有执行任何操作。没有任何内容被放到Word文档中)

For i = 0 To 1
    Dim node As Object //declaring variable
    Set node = oXMLFile.SelectNodes("root/options/option_element[i]/value/text()") 
    For j = 0 To node.Length - 1
        .Content.InsertAfter node(j).NodeValue 
     Next j
    .Content.InsertParagraphAfter //this just adds a new line to the doc
    Set node = Nothing
Next i

(使用Excel VBA宏检索XML)

1 个答案:

答案 0 :(得分:3)

x=0
while [[ $x -lt 10 ]]; do
    x=$((x+1))
    printf '%s\r'"Processing page ${x}"
    sleep 1
done

应该是

Set node = oXMLFile.SelectNodes("root/options/option_element[i]/value/text()")

在您的第一个示例中,您使用的是文字“i”,而不是Set node = oXMLFile.SelectNodes("root/options/option_element[" & i & "]/value/text()")