对象变量或with块变量未设置
Private Sub Form_Load()Form1.MousePointer = 11
WebBrowser1.Navigate "http://weather.tq121.com.cn/detail.php?city=北京" ' 起始网址
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Tables As IHTMLElementCollection
Set Tables = WebBrowser1.Document.getElementsByTagName("Table")
Dim i As Integer
Dim j As Integer
Dim Table1 As HTMLTable
For Each Table1 In Tables
Next
Dim Row As HTMLTableRow, Cell As HTMLTableCell
j = Table1.rows.Length - 2
For i = 1 To Table1.rows.Length - 1 ' 逐行处理
Set Row = Table1.rows(i)
j = 0
For Each Cell In Row.cells ' 逐列处理
' Row.cells(j).innerText即为当前行及当前列上的单元数据
Text1.Text = Text1.Text + Trim(Row.cells(j).innerText) + ","
j = j + 1
Next
' 一行处理完毕后,去除行尾的逗号并加上回车
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) + vbCrLf
Next
End Sub
上面是抓取网页上信息的代码,但是运行到For i = 1 To Table1.rows.Length - 1 ' 逐行处理 这一句时就报“对象变量或with块变量未设置”的错,头都大了。
--------------------编程问答-------------------- 在WebBrowser1下载网页的过程中,有可能多次触发DocumentComplete事件,而此时网页并没有完成,可能还没有HTMLTable对象,当然会出错了,在DocumentComplete事件子程序中加入这样一句试试:
--------------------编程问答-------------------- 加上程序跳出以后再也不触发DocumentComplete事件了 --------------------编程问答-------------------- 招聘兼职(网上市场调研专员)
if instr(url,"http://weather.tq121.com.cn/detail.php?city=北京")=0 then exit sub
职位类型:兼职
招聘人数:不限
公司介绍:本公司是一家专业的市场调研服务公司,协助各大国际市场调研公司在中国招聘网上市场调研专员。
岗位要求:诚实,有耐心!从事过市场调查方面工作的优先。
工作内容:我们会通过电子邮件,不定期发送调查问卷给调研专员,调研专员自己安排时间填写问卷就可以了。收入按完成问卷的数量计算,每份20分钟的问卷报酬大约为10元。
详情请访问:http://www.freesurvey.net.cn?SOURCE-9F4CDF18-6995-457A-8F13-800046345882(注册用户后可联系我本人)
QQ咨询 :86037127
电子邮件:bvtc_lgy@163.com --------------------编程问答-------------------- 你检查一下table1里面的内容 --------------------编程问答-------------------- Private Sub Form_Load()
Form1.MousePointer = 11
WebBrowser1.Navigate "http://weather.tq121.com.cn/detail.php?city=北京 " ' 起始网址
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Tables As IHTMLElementCollection
Set Tables = WebBrowser1.Document.getElementsByTagName( "Table ")
Dim i As Integer
Dim j As Integer
Dim Table1 As HTMLTable
For Each Table1 In Tables
Next (这个应该是外层循环,楼主的TABLE1 应该在这层循环内有效,而按你现在的写法,这个循环没有执行任何语句)
Dim Row As HTMLTableRow, Cell As HTMLTableCell
j = Table1.rows.Length - 2 ‘这里已经退出循环,当然是无效了。
For i = 1 To Table1.rows.Length - 1 ' 逐行处理
Set Row = Table1.rows(i)
j = 0
For Each Cell In Row.cells ' 逐列处理
' Row.cells(j).innerText即为当前行及当前列上的单元数据
Text1.Text = Text1.Text + Trim(Row.cells(j).innerText) + ", "
j = j + 1
Next
' 一行处理完毕后,去除行尾的逗号并加上回车
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) + vbCrLf
Next
End Sub
补充:VB , 基础类