简体   繁体   English

为什么我不断收到类型不匹配错误?

[英]Why do I keep getting a type mismatch error?

The asterisks indicate where the error is occuring Below is he code that I am using星号表示错误发生的位置下面是我正在使用的代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Row = Range("Item_ID").Row And _
    Target.Column = Range("Item_ID").Column Then
    Dim ie As New InternetExplorer
    
    'Set IE.Visible
    ie.Visible = True
    
    ***ie.navigate "https://portal.shiptrackapp.com/Admin/reports.aspx?records=" & Range("Item_ID").Value***
    
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE
    Dim Doc As HTMLDocument
    Set Doc = ie.document
    Dim odd As String
    odd = Trim(Doc.getElementsByClassName("odd")(1).innerText)
    ie.Quit
    Dim aodd As Variant
    aodd = Split(sodd, ",")
    Range("Tracking_Number").Value = aodd(3)
    Range("Item_ID").Value = aodd(0)
    Range("pick_up_date").Value = aodd(2)
    Range("Delivery_Status_Date").Value = aodd(3)
    Range("Time").Value = aodd(4)
    Range("Delivery_Driver_Name").Value = aodd(5)
    Range("Status").Value = aodd(6)
    Range("Stats_des").Value = aodd(7)
    Range("Comments").Value = aodd(8)
    Range("Signed_By").Value = aodd(9)
    Range("Del_Add").Value = aodd(10)
    Range("History").Value = aodd(11)
    Range("Transaction_ID").Value = aodd(12)
    Range("ScanCode_dt").Value = aodd(13)
    Range("Company_Name").Value = aodd(14)
    Range("Def_loc_id").Value = aodd(15)
    Range("Dri_cmmts").Value = aodd(16)
    Range("creation_dt").Value = aodd(17)
    Range("scan_type").Value = aodd(18)
End If
End Sub

You may have modelled your code on this site , where the syntax appears 100% identical with yours.您可能已经在此站点上为您的代码建模,其中的语法与您的语法 100% 相同。

Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
Dim ecoll As Object
ie.Visible = True
ie.navigate "http://demo.guru99.com/test/web-table-element.php"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.documen

The line of code that gives the trouble has a concatenated string.出现问题的代码行有一个连接字符串。 I suggested you eliminate that and try your code with a hard-coded string as shown above.我建议您消除它并使用硬编码字符串尝试您的代码,如上所示。 If that works the problem must be caused by the concatenation: either the variable read from the worksheet or the concatenation syntax itself.如果可行,则问题一定是由连接引起的:从工作表中读取的变量或连接语法本身。 Please do the first test first.请先做第一个测试。

I don't use IE myself.我自己不使用IE。 Here is working code I use every day.这是我每天使用的工作代码。 Note that the argument HTMLdoc is a return object.请注意,参数HTMLdoc是一个返回对象。 It is passed to the function empty, the function fills it up and the calling procedure receives it back.它被传递给空函数,函数将它填满,调用过程将它接收回来。

Function GetHTMLDoc(ByVal Site As Nur, _
                    HTMLdoc As MSHTML.HTMLDocument, _
                    Optional Secty As String) As Boolean
    ' NIC 003 ++ 10 Aug 2020
    ' return Not True if the site couldn't be reached

    Dim URL         As String
    Dim Response    As String
    
    URL = StoredURL(Site, Secty)
        ' URL is a string like "https://portal.shiptrackapp.com/Admin/reports.aspx?records=MAA12345"
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False                 ' not asynchronous
        On Error GoTo NoURL
        .Send
        Response = StrConv(.responseBody, vbUnicode)
    End With
    
    On Error GoTo 0
    Set HTMLdoc = New HTMLDocument
    HTMLdoc.Body.innerHTML = Response
    GetHTMLDoc = True
    Exit Function
    
NoURL:
    Msg.InfoBox "NoURL", 0, vbInformation, URL    ' this is a MsgBox
    Err.Clear
End Function

You may like to google for the "MSXML2.XMLHTTP" object to find code that handles the HTMLdocument.您可能喜欢在 google 中搜索“MSXML2.XMLHTTP”对象以查找处理 HTML 文档的代码。 Basically, "MSXML2.XMLHTTP" connects to the Internet without the use of a browser.基本上,“MSXML2.XMLHTTP”无需使用浏览器即可连接到 Internet。 However, changing the system shouldn't solve your problem because that lies with the variable URL (in my code) which you will need in whichever system you deploy.但是,更改系统不应解决您的问题,因为这取决于在您部署的任何系统中都需要的变量URL (在我的代码中)。 Therefore I recommend that you try your existing code without the concatenation.因此,我建议您在没有串联的情况下尝试现有代码。

To take this one step further: If the hard-coded string also fails then the problem might be with the IE.更进一步:如果硬编码字符串也失败,那么问题可能出在 IE 上。 You may have to load a reference library (DLL) to use it.您可能需要加载参考库 (DLL) 才能使用它。 In that case the surprise would be that no error occurs at ie.Visible = True or even Dim ie As New InternetExplorer and the possibility of using another system appears in a new light.在这种情况下,令人惊讶的是在ie.Visible = True或什至Dim ie As New InternetExplorer处没有错误发生,并且使用另一个系统的可能性以一种新的方式出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM