【问题标题】:VBA - Object variable or with block variable not set. Variable as IntegerVBA - 对象变量或未设置块变量。变量为整数
【发布时间】:2016-06-01 21:08:27
【问题描述】:

我在以下代码中的If document.getElementsByClassName("prod-summ")(tdNum).getElementById("no-piece").FirstChild.NodeValue = ItemNbr Then 行不断收到“运行时错误'91':对象变量或未设置块变量”:

Dim tdNum As Integer
Dim ItemNbr As String

    'More code here

tdNum = 0
    If document.getElementsByClassName("prod-summ")(tdNum).getElementById("no-piece").FirstChild.NodeValue = ItemNbr Then
        Cells(cell, 2).Value = document.getElementsByClassName("prod-summ")(tdNum).getElementById("col-action").getElementsByTagName("span")(0).innerText
    Else
        tdNum = tdNum + 1
    End If

我四处搜索,但似乎找不到对我的代码有意义的解决方案。

【问题讨论】:

  • 作为解决这些问题的一般方法,在该行设置断点。然后使用即时窗口调查方法/属性链中的问题所在(例如,document 是否设置为对象?getElementsByClassName( 是否返回任何内容?)
  • 你能有两个这样的论点吗? getElementsByClassName("prod-summ")(tdNum)
  • 你可以。 ("prod-summ") 是 ClassName,(tdNum) 是一个数字变量。所以第一个实例是getElementsByClassName("prod-summ")(0),它针对具有类“prod-summ”的元素的第一个实例

标签: html vba


【解决方案1】:

我解决了我的问题。首先,有一个错字。而不是getElementsByClassName("prod-summ"),应该是getElementsByClassName("prod-somm")

而且我猜对象层次结构不喜欢在除了document 之外的任何东西后面加上getElementById,所以用以下代码替换我的代码:

For tdNum = 0 To 10

    If document.getElementsByClassName("prod-somm-texte")(tdNum).FirstChild.FirstChild.NodeValue = ItemNbr Then
        Cells(cell, 2).Value = document.getElementsByClassName("prix")(tdNum).FirstChild.innerText
        Exit For
    Else
        Debug.Print document.getElementsByClassName("prod-somm-texte")(tdNum).FirstChild.FirstChild.NodeValue
    End If

Next tdNum

这似乎解决了它。

谢谢大家的意见,真的很有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多