使用 Excel VBA 创建 IE 对象时出现运行时错误

问题描述 投票:0回答:1

我想从网站获取数据。
当我单击创建的同步按钮时,它第一次运行。 下次点击时,会显示如下错误。

Error Image

它卡在了

CreateObject
,我的代码如下。

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application") // Error here on second run/click

IE.Visible = True
IE.Silent = True
IE.Navigate2 URL

Do While IE.Busy = True Or IE.ReadyState <> READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("00:00:02")) 'Wait 2 second, then check again.
Loop
  
'Do my logic here
    
IE.Quit
Set IE = Nothing

我试过了,但问题是一样的。

Set IE = New InternetExplorer // Error here on second run/click

Set IE = New InternetExplorerMedium // Error here on second run/click
excel vba internet-explorer
1个回答
1
投票

快速谷歌一下这个问题表明你需要杀死所有 IE 进程。如果您在最初的研究中没有遇到过它,我总结如下。这是一个“敲开坚果的大锤”,我不知道为什么需要这样做,但你可能想尝试一下,看看它是否适合你。

在启动新对象的代码之前添加以下行:

Call Shell("cmd.exe taskkill /F /IM /c iexplore.exe") 
© www.soinside.com 2019 - 2024. All rights reserved.