网页上的VBA会在HTML文档中引发自动化,未指定的错误

时间:2014-10-24 08:11:04

标签: vb.net vba excel-vba internet-explorer-9 excel-2010

我正在为网页做自动化。我只需要做一个简单的任务(在页面上设置一个值),但我无法让它工作。这是我的代码的缩减。

Dim IExp As SHDocVw.InternetExplorer

Dim hDoc As HTMLDocument
Dim hCol As MSHTML.IHTMLElementCollection
Dim hInp As MSHTML.HTMLInputElement
Dim hPoint As MSHTML.tagPOINT

Set IExp = New SHDocVw.InternetExplorer
IExp.Visible = True
IExp.navigate "http://somesite.com/"

Do Until IExp.Busy = False
    DoEvents
Loop

Set hDoc = IExp.document

' Find the "search for" input box on the page
Set hCol = hDoc.getElementsByTagName("input")
For Each hInp In hCol

代码在此行引发异常

Set HTMLdoc = iExp.Document

错误说:

Run-time error '-2147467259 (80004005)':
Automation error
Unspecified error

我只是希望它在网页上抛出一个字符串。

可能会有所帮助的事情:

 - Just need to run the htmldoc line without error
 - By the way, im using IE9 and MS Excel 2010.  I added vb.net tag as I know I could edit some syntax to get it to work with vba.
 - I tried changing my references and the declared variables.  I used MSXML2.XMLHTTP60, and also the code with CreateObject("InternetExplorer.Application") but they also don't work.

先谢谢你们!

1 个答案:

答案 0 :(得分:0)

嗯,我认为问题可能出在您所引用的内容上,而且您没有指定引用HTMLDocument的库:

尝试更改

  Dim hDoc As HTMLDocument

要:

  Dim hDoc As MSHTML.HTMLDocument

如果对其进行排序,请确保您没有引用具有HTMLDocument对象的多个库。

我刚刚测试了Office 2013中引用的代码' Microsoft Internet Controls'和' Microsoft HTML对象库'只是它对我来说很好。

当我使用xDocument对象使用不同的XML库引用时,我确实遇到过类似的问题。