Declaring variable as Internet Explorer generates

2019-08-29 04:58发布

问题:

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.

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

Code Below:

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

回答1:

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 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")

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

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 info on early/late binding:

Using early binding and late binding in Automation



回答2:

Excel needs to know what the objects are in your VBA code. If it encounters an unknown type, then you'll need to add a reference to that object's code (DLL). Add a reference in VBA by clicking on Tools -> References. All the COM libraries that are registered on your computer will show up in the list automatically.

To get the InternetExplorer object to work, add a reference to Microsoft Internet Controls

To get the HTMLLinkElement object to work, add a reference to Microsoft HTML Object Library