我遇到了
"对象变量或未设置块变量"
错误。
当我尝试设置Table = htmldoc.getElementsbytagname("tr")(116).getelementsbytagname("td")
时会发生这种情况。它有时会起作用,但有时我会收到此错误。关于如何避免遇到此错误的任何想法?我在我的代码中注意到错误发生并包含下面的html。
Sub gethourlymaxvalues()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
With IE
.Visible = False
.navigate "webaddress"
Do Until .readyState = 4
DoEvents
Loop
.document.all.item("Login1_UserName").Value = "username"
.document.all.item("Login1_Password").Value = "pw"
.document.all.item("Login1_LoginButton").Click
Do Until .readyState = 4
DoEvents
Loop
.navigate "webaddress"
Do Until .readyState = 4
DoEvents
Loop
Dim htmldoc As Object
Dim aTable As Object
Dim r
Dim c
Do Until IE.readyState = 4
DoEvents
Loop
Set htmldoc = IE.document
Error 91>> Set aTable = htmldoc.getElementsByTagName("tr")(116).getElementsByTagName("td") ''Error object variable or with block variable not set''
r = 0
c = 0
For Each TDelement In aTable
ThisWorkbook.Sheets("Slice").Range("j8").Offset(r, c).Value = TDelement.innerText
r = r + 1
Next
r = 0
Set IE = Nothing
End Sub
HTML:
</table>
</tbody>
<td>
<span id="ctl00_PageContent_LblCompany" style="font-family: Verdana;">Company</span></td><td><select name="ctl00$PageContent$cboCompanies" id="ctl00_PageContent_cboCompanies" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$cboCompanies\',\'\')', 0)">
<option value="AA">AVA</option>
<option value="CD">CHPD</option>
<option value="CS1">COMS1</option>
</select><input name="ctl00$PageContent$cmdViewReport" id="ctl00_PageContent_cmdViewReport" type="submit" value="View Report"><input name="ctl00$PageContent$cmdDownload" id="ctl00_PageContent_cmdDownload" type="submit" value="Download Excel"></td><td><select name="ctl00$PageContent$cboRefresh" id="ctl00_PageContent_cboRefresh" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$cboRefresh\',\'\')', 0)">
<option value="0">Auto Refresh Off</option>
<option selected="selected" value="900000">Auto Refresh: 15 Minutes</option>
<option value="1800000">Auto Refresh: 30 Minutes</option>
</select></td>
</tr>
</tbody></table>
<table id="ctl00_PageContent_Table2" border="0"><tbody>
</tr><tr style="background-color: palegoldenrod;">
<td><span id="ctl00_PageContent_grdReport_ctl05_Label50">Estimated Calculated Max Cap</span>
</td><td>
<span id="ctl00_PageContent_grdReport_ctl05_Label51">161</span>
</td>
</tr><tr style="background-color: beige;">
<td>
<span id="ctl00_PageContent_grdReport_ctl06_Label50">Estimated Pond Min Capacity Restriction</span>
</td><td>
<span id="ctl00_PageContent_grdReport_ctl06_Label51">159</span>
</td><td>
<span id="ctl00_PageContent_grdReport_ctl06_Label52">159</span>
</td>
</tr><tr style="background-color: beige;">
<td><span id="ctl00_PageContent_grdPond_ctl19_Label50">Participant Pond Maximum</span>
</td><td><span id="ctl00_PageContent_grdPond_ctl19_Label51">2630</span>
</td>
</td>
</tr>
</tbody>
</table>
&#13;