为什么我的代码在添加"for loop"时突然停止工作(使用 VBA 检索 XML)



这段代码有效:

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(

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

应该是

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

在您的第一个示例中,您使用的是文字"i",而不是 i 的值

最新更新