首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在VBA中设置iDoc = IE.Document会出现运行时错误

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

Stack Overflow用户
提问于 2015-11-20 00:03:06
回答 1查看 8.7K关注 0票数 3

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

对我来说,set iDoc = IE.Document在下面的代码片段中工作得很好。但是对于最终用户,我得到了“类型不匹配错误”。

下面是我的代码:

代码语言:javascript
复制
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在我们两个人身上都能正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-20 01:26:36

虽然不是IE特有的,但this MS article暗示了VBA和x64系统中的基础API调用存在已知问题。

对于外部/自定义应用程序接口调用,我们可以使用PtrSafeLongPtr声明来适应这一点。

使用后期绑定,我发现它在过去对我很有效:

代码语言:javascript
复制
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

缺点是你将失去intellisense特性--但是我猜如果代码不能运行,IntelliSense就没有那么有用了……

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33808763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档