在VBA中定义MSHTML.HTMLWindow2对象时键入不匹配错误

时间:2017-02-16 08:59:53

标签: excel vba excel-vba

我在VBA中定义 MSHTML.HTMLWindow2 对象时遇到类型不匹配错误。请找到我的下面的代码。

Sub GoToWebsiteTest() 
    Dim appIE As InternetExplorerMedium
    Dim tags As Object
    Dim tagx As Object
    Dim repo As Object
    Dim Element As HTMLButtonElement
    i = 0
    Set appIE = New InternetExplorerMedium
    sURL = "http://myurl/blink/"
    With appIE
        .navigate sURL
        .Visible = True
    End With

    Do While appIE.Busy Or appIE.READYSTATE <> 4
        DoEvents
    Loop

        appIE.document.getElementById("Username").Value = "xxx"
        appIE.document.getElementById("Pwd").Value = "xxxx"
       Set tags = appIE.document.getElementsByTagName("Button")
       tags(i).Click

       Do While appIE.Busy Or appIE.READYSTATE <> 4
        DoEvents
    Loop

    Application.Wait Now + TimeSerial(0, 0, 5)

    Dim iFrm As MSHTML.HTMLElementCollection
    'Dim iFrm As MSHTML.HTMLWindow2 ''' Wrong Object Defined
    Dim HTMLdocc As HTMLDocument

    Set iFrm = appIE.document.frames("MAIN").document.getElementById("topmenu")
    Set iFrm2 = iFrm.getElementsByTagName("table")
    x = 1
    For Each Item In iFrm2
    On Error Resume Next
    If Item.ID = "table10" Then
    iFrm3 = Item.getElementsByTagName("a")
    iFrm3.Click
    End If
    On Error GoTo 0
    Next

    Application.Wait (Now + TimeValue("00:00:5"))

直到我的代码以上工作完全正常。当我进一步添加以下代码时,我在将 opn 定义为 MSHTML.HTMLWindow2 对象时面临类型不匹配错误。

 'Dim opn As Object
 Dim opn As MSHTML.HTMLElementCollection

Set opn = appIE.document.frames("MAIN").document.getElementsByTagName("table")

0 个答案:

没有答案