VB6模块中声明WebBrowser对象报错,求正确声明方案
在VB6模块中正确使用WebBrowser对象的解决方案
嗨,作为VB6新手遇到这个问题很正常,我来帮你理清思路并解决它!
你遇到的Object Required错误,本质是因为模块本身没有直接访问窗体上WebBrowser控件的权限——WebBrowser是依附于窗体的控件,模块默认不知道它的存在;而你尝试的WebBrowser_V1是早期的接口,缺少Navigate2这类方法,所以会触发另一个错误。
下面给你两种可行的解决方案,按需选择:
方案1:将窗体上的WebBrowser控件作为参数传递给模块函数
这是最直接的方法,既然控件在窗体上,我们把它作为参数传给模块的Paises函数即可。
修改模块代码
Sub Paises(clifor, tempodia, WB As WebBrowser) Dim db As New ADODB.Connection Dim RS As ADODB.Recordset ' 建议显式声明Recordset,避免隐式变量 Dim SQL As String, Pais As String Dim t As Single, checktime As Single db.Open "Driver={MySQL ODBC 3.51 Driver};Uid=root;Pwd=03072003;Server=xxx.xxx.xx.xx;Port=xxx;Option=xxx;STMT=;Database=xxx" SQL = "select cd_ocorrencia, vl_latitude, vl_longitude from ocorrencias where dt_hora >= date_format(date_add(current_timestamp, interval " & tempodia & " day), '%Y/%m/%d') " SQL = SQL & " and cd_status <> 99 and ds_pais = '' and vl_latitude <> '0.000000' and vl_longitude <> '0.000000' order by cd_ocorrencia asc " Set RS = db.Execute(SQL) While Not RS.EOF WB.Navigate2 "http://nominatim.openstreetmap.org/reverse?format=xml&lat=" & Replace(RS!vl_latitude, ",", ".") & "&lon=" & Replace(RS!vl_longitude, ",", ".") & "&zoom=18&addressdetails=1" t = Timer Do Until WB.ReadyState = READYSTATE_COMPLETE checktime = Timer - t If checktime >= 30 Then WB.Stop DoEvents End If DoEvents Loop If InStr(WB.Document.body.innertext, "A página XML não pode ser exibida") = 0 Then Pais = UCase(Mid(WB.Document.body.innertext, InStr(WB.Document.body.innertext, "</country_code>") - 2, 2)) db.Execute ("update ocorrencias set ds_pais = '" & Pais & "' where cd_ocorrencia = " & RS!cd_ocorrencia) End If Pais = "" RS.MoveNext Wend RS.Close db.Close Set RS = Nothing Set db = Nothing End Sub
修改窗体调用代码
假设你窗体上的WebBrowser控件名叫WB,调用时直接传递控件实例:
Private Sub Form_Load() Me.Top = 585 Me.Left = 12090 ' 传递窗体上的WebBrowser控件作为参数 Call BuscaPais.Paises(48, -1, Me.WB) End Sub
方案2:在模块中动态创建WebBrowser对象(无需窗体控件)
如果不想依赖窗体上的控件,可以动态创建一个后台的WebBrowser实例(本质是IE内核的对象),但需要注意它需要一个窗口容器,我们可以直接创建IE对象并设置为隐藏状态。
模块代码示例
Sub Paises(clifor, tempodia) Dim WB As Object ' 用Object兼容,或者引用Microsoft Internet Controls后用WebBrowser Dim db As New ADODB.Connection Dim RS As ADODB.Recordset Dim SQL As String, Pais As String Dim t As Single, checktime As Single ' 动态创建IE内核的浏览器对象,设置为不可见 Set WB = CreateObject("InternetExplorer.Application") WB.Visible = False ' 避免弹出浏览器窗口 db.Open "Driver={MySQL ODBC 3.51 Driver};Uid=root;Pwd=03072003;Server=xxx.xxx.xx.xx;Port=xxx;Option=xxx;STMT=;Database=xxx" SQL = "select cd_ocorrencia, vl_latitude, vl_longitude from ocorrencias where dt_hora >= date_format(date_add(current_timestamp, interval " & tempodia & " day), '%Y/%m/%d') " SQL = SQL & " and cd_status <> 99 and ds_pais = '' and vl_latitude <> '0.000000' and vl_longitude <> '0.000000' order by cd_ocorrencia asc " Set RS = db.Execute(SQL) While Not RS.EOF WB.Navigate2 "http://nominatim.openstreetmap.org/reverse?format=xml&lat=" & Replace(RS!vl_latitude, ",", ".") & "&lon=" & Replace(RS!vl_longitude, ",", ".") & "&zoom=18&addressdetails=1" t = Timer Do Until WB.ReadyState = READYSTATE_COMPLETE checktime = Timer - t If checktime >= 30 Then WB.Stop DoEvents End If DoEvents Loop If InStr(WB.Document.body.innertext, "A página XML não pode ser exibida") = 0 Then Pais = UCase(Mid(WB.Document.body.innertext, InStr(WB.Document.body.innertext, "</country_code>") - 2, 2)) db.Execute ("update ocorrencias set ds_pais = '" & Pais & "' where cd_ocorrencia = " & RS!cd_ocorrencia) End If Pais = "" RS.MoveNext Wend ' 一定要释放资源,避免内存泄漏 RS.Close db.Close WB.Quit ' 关闭IE进程 Set WB = Nothing Set RS = Nothing Set db = Nothing End Sub
额外提醒
- 动态创建的方法适合后台批量处理,但要记得调用
WB.Quit并释放对象,否则会残留IE进程占用内存。 - 访问Nominatim时,建议在每次请求后添加1-2秒的延迟(比如用
Sleep 1000,需要提前声明Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)),避免触发其反爬限制导致请求失败。
内容的提问来源于stack exchange,提问作者A52
相关产品推荐
相关产品推荐

