设置iDoc = IE.Document在VBA中给出运行时错误

时间:2015-11-19 16:03:06

标签: vba excel-vba web-scraping ie-automation excel

我有一个奇怪的问题。我使用的是32位版本的IE 10.最终用户使用的是64位版本IE10

对我来说set iDoc = IE.Document在以下代码段中运行正常。但是对于最终用户,我得到了#34;类型不匹配错误"。

以下是我的代码:

Function Run() As Integer
    Dim IE As InternetExplorer
    Dim dataCount%

    Set IE = GetIE

    Navigate IE, "http://www.my-url-here.com/index.php"
    Call Login(IE)
    IE.Quit
End Function

Private Sub Login(IE As InternetExplorer)
    Dim iDoc As HTMLDocument
    Dim uName$, pwd$
    Set iDoc = IE.Document   ' here is where it gives type mismatch error

    Call GetLoginDetails(jobBoard, uName, pwd)

    iDoc.getElementById("login").Value = uName
    iDoc.getElementById("pw").Value = pwd
    iDoc.getElementsByClassName("sub_btn")(0).Click

    Sync IE
End Sub

    Sub Sync(IE As InternetExplorer)
        Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
            Application.Wait Now + TimeSerial(0, 0, 1)
        Loop

        Do While IE.Document.ReadyState <> "complete"
            Application.Wait Now + TimeSerial(0, 0, 1)
        Loop

        'Debug.Print "Out: " & IE.Document.ReadyState
    End Sub

    Sub Navigate(IE As InternetExplorer, address$)
        IE.Navigate address
        Sync IE
    End Sub

    Function GetIE() As InternetExplorer
        Set GetIE = New InternetExplorer

        With GetIE
            .Visible = True

            .Height = 550
            .Width = 800

            .Left = Application.Width - .Width
        End With
    End Function

请注意:IE.Document.getElementById("login").Value = uName对我们双方都有效。

1 个答案:

答案 0 :(得分:4)

虽然不是特定于IE,this MS article意味着VBA和x64系统中存在潜在API调用的已知问题。

通过外部/自定义API调用,我们可以使用PtrSafeLongPtr声明来解决这个问题。

使用后期绑定,我发现过去曾为我工作过:

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

缺点是您将失去功能 - 但如果代码没有运行,智能感知并不是真的有用......我猜...