在Internet Explorer生成变量时声明变量编译错误:未定义用户定义的类型 [英] Declaring variable as Internet Explorer generates Compile error: user-defined type not defined

查看:243
本文介绍了在Internet Explorer生成变量时声明变量编译错误:未定义用户定义的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来调整我的代码。运行此命令时,出现错误,提示编译错误:用户定义的类型未定义,位于 Dim objIE作为Internet Explorer 行。

I need some help adjusting my code. When running this, I get an error saying Compile error: user-defined type not defined on the line Dim objIE As Internet Explorer.

如果我删除了上面的行,对于 Dim aEle和HTMLLinkElement ,我将收到相同的错误。

If I remove the line above, I get the same error for Dim aEle as HTMLLinkElement.

以下代码:

Sub SearchBot()

Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim y As Integer
Dim result As String
Dim tbl As Object, obj_tbl As Object
Dim lastRow As Long
Dim start_time As Date
Dim end_time As Date
Dim cookie As String
Dim result_cookie As String

Set objIE = New InternetExplorer

objIE.Visible = True

objIE.navigate "https://website.com"

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

For i = 2 To 1829


objIE.document.getElementById("SearchTopBar").Value = _
Sheets("Sheet2").Range("A" & i).Value  '

Set oNode = objIE.document.getElementsByClassName("iPadHack tmbsearchright") 
(0)
oNode.Click

On Error GoTo ErrorHandler

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

Dim tblEle As Object
Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(5)

Sheets("Sheet2").Range("B" & i).Value = tblEle.innerText
Debug.Print tblEle.innerText

Next

On Error GoTo ErrorHandler

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

Dim tblEle As Object
Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(5)

Sheets("Sheet2").Range("B" & i).Value = tblEle.innerText
Debug.Print tblEle.innerText

NextItem:
Next

ErrorHandler

Resume NextItem

Resume Next

objIE.Quit
End Sub


推荐答案

如前所述,如果要使用早期绑定代码,则需要添加适当的参考库,因此程序知道它要处理的内容并可以访问适当的属性和方法。 Microsoft Internet控件 Microsoft HTML对象库都是标准的,您可以将这些引用添加到您所在的项目中分发给可能使用同一Microsoft应用程序不同版本的其他用户,而不必担心由于版本控制而丢失参考错误。

As mentioned, if you are going with early bound code then you will need to add the appropriate reference libraries so your program knows what it is dealing with and has access to the appropriate properties and methods. Both Microsoft Internet Controls and Microsoft HTML Object Library are "standard" and you can add these references to a project you are distributing to other users, who may be using different versions of the same Microsoft Application, without fear of the missing reference errors due to versioning.

Microsoft在其中付出了很多钱一个反托拉斯解决方案,以便每个人都拥有MSHTML / IE。另请参见美国诉Microsoft公司

Microsoft paid a lot of money in an anti-trust settlement in order for everyone to have MSHTML/IE. See also United States v. Microsoft Corp. Credit to user ThunderFrame for educating me on this.

通常的解决方法,特别是对于其他依赖版本的库,是为开发编写早期绑定的代码,当您访问智能感知和完整的库以进行更快的开发时(请查看优点/差异的完整列表)。为了分发,可以将引用转换为后期绑定,以避免版本问题。正如我说的那样,对于提到的标准两个库来说,这实际上不是问题,但值得知道它们的区别以及后期绑定的版本是:

The typical way round this, particularly with other libraries that are version dependant, is to write early bound code for your development, as you get access to intellisense and the full library for faster development (look in to the full list of benefits/differences). For distribution, one converts references to late bound to avoid the versioning problems. As I said this shouldn't really be such a problem for the "standard" two libraries mentioned, but worth knowing the difference and that the late bound versions are:

Dim IeApp As Object
Set IeApp As CreateObject("InternetExplorer.Application")

Dim hFile As Object
Set hFile = CreateObject("htmlFile")

请注意,声明的类型为 object 。在这种情况下,变量名只是个人选择。

Notice that the declarations are of type object. The variable names are just a personal choice in this instance.

特别是,晚绑定的htmlFile调用将公开更多受限制的接口,并且您将拥有较少的常用接口

In particular, a late bound htmlFile call will expose a much more limited interface and you will have less of the commonly used methods and properties associated with the early bound class.

Microsoft早期/晚期绑定信息:

在自动化中使用早期绑定和晚期绑定

这篇关于在Internet Explorer生成变量时声明变量编译错误:未定义用户定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆