首页 > 解决方案 > Set doc = IE.document 返回自动化错误

问题描述

我正在尝试使用一组填充此表单(https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood) excel 数据,用 Excel 中的相应列填充表单的每个字段。我在 StackOverflow 和 Youtube 上看到了几个关于使用 VBA 打开 IE、导航和填写文本框的教程,我从中编写了这些代码。

当我运行此代码并且 Excel 返回一个错误消息框时出现问题

运行时错误 '-2147467259 (80004005) 自动化错误未指定错误。

我太业余了,无法诊断这些在 Youtube 上的各种视频上完美运行的代码。希望你们能帮助我:)

Sub IE()

    Dim IE As InternetExplorerMedium
    Set IE = New InternetExplorerMedium

    Dim doc As HTMLDocument
    Set doc = IE.document    //The error I mentioned above is in this line

    IE.Visible = True
    IE.navigate "https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood"

    Do While IE.Busy
        DoEvents
    Loop

    For Each ele In IE.document.getElementsByClassName("ant-radio-input")
        If ele.Value = ThisWorkbook.Sheets("Sheet1").Range("A1") Then ele.Click: Exit For
    Next

    doc.getElementById("phone").Value = ThisWorkbook.Sheets("Sheet1").Range("C2").Value
    doc.getElementById("email").Value = ThisWorkbook.Sheets("Sheet1").Range("D2").Value

    IE.Quit

End Sub

标签: vbainternet-explorermethods

解决方案


在创建 IE 实例并加载文档之前,您无法设置 ie.document。

IE.Visible = True
IE.navigate "https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood"
While IE.Busy Or IE.ReadyState <> 4:DoEvents:Wend
Set doc = IE.document 

推荐阅读