P粉5174756702023-09-03 18:13:40
IE is dead and should no longer be used. However, it is also possible to use the shortened URL https://cpc.farnell.com/SD02808< /a> via xhr (XML HTTP request). The following code only shows how to read the required information from the loaded HTML code. All safeguards are missing.
The object error occurs in the code shown above because you are trying to set a variable of data type String
to Nothing
, just like an object variable using Set strData = none
.
Sub GetCPCOfferInfos() Dim url As String Dim doc As Object Set doc = CreateObject("htmlFile") With CreateObject("MSXML2.XMLHTTP.6.0") url = "https://cpc.farnell.com/" & ActiveSheet.Cells(1, 1) .Open "GET", url, False .Send If .Status = 200 Then doc.body.innerHTML = .responseText ActiveSheet.Range("A3") = doc.getElementsByTagName("h1")(0).innertext _ & doc.getElementsByTagName("h2")(0).innertext 'Title ActiveSheet.Range("B3") = doc.getElementsByClassName("ManufacturerPartNumber")(0).innertext 'Manufacturer Part Number ActiveSheet.Range("C3") = doc.getElementById("pdpSection_FAndB").innertext 'Product Overview ActiveSheet.Range("D3") = doc.getElementById("pdpSection_pdpProdDetails").innertext 'Product Information Else MsgBox "Page not loaded. HTTP status " & .Status End If End With End Sub