Excel VBA:自动化错误 – 远程过程调用失败

我从网站提取某些数据。 我必须从一个表中至less执行百万行这个任务。 我正在使用Excel VBA与MySQL连接。

  • 使用MySQL与Excel VBA连接,我从表中获取作者的名字,姓氏。
  • 对于作者的名字,姓氏,我将Linkedin添加到search查询中,并在Google中search。
  • 从search结果中,我打开HTML格式的第一个search结果页面并提取一些信息。
  • 我把一些提取的信息放回到MySQL表中。

一切工作正常按照上述步骤。 但是,如果我尝试做超过10行,我得到以下错误。

自动化错误远程过程调用失败,并没有执行

我意识到这是关于IE的打开/closures。 在我的程序中,我有以下代码。

要创build一个新的IE应用程序,我已经定义如下。

Set ie = New InternetExplorer Set RegEx = New RegExp Dim iedoc As Object ie.Navigate "http://www.google.com/search?hl=en&q=" & FirstName & "+" & LastName & "+linkedin&meta=" Do Until ie.ReadyState = READYSTATE_COMPLETE Loop MyStr = ie.Document.body.innerText Set RegMatch = RegEx.Execute(MyStr) 

在提取一个作者的数据之后,我在下面有一段代码。

 ie.Quit Set RegEx = Nothing Set ie = Nothing Dim strBatchName As String strBatchName = "F:\command.bat" Shell strBatchName 

command.bat具有以下代码。

 taskkill.exe /F /IM iexplore.exe /T 

如果我的表中less于10行,它工作得很好。 但是,对于更多的行我得到上述错误。

我会更倾向于使用ie的相同实例来获取所有的作者数据,像下面的例子一样遍历它们。 另外,你的batch file是用什么方式处理的? 我从来没有发现需要batch file来帮助执行vba。 你用它们来写文本文件? 你也可以用vba来完成。

 Dim ie As Object, lastRowAuthors as long, i as long, strBatchName As String lastRowAuthors = sheets("Authors").Cells(Rows.Count, 1).End(xlUp).row Set ie = New InternetExplorer for i = 1 to lastRowAuthors ie.Navigate "http://www.google.com/search?hl=en&q=" & sheets("Authors").range("A" & i).value & "+" & sheets("Authors").range("B" & i).value & "+linkedin&meta=" Do Until ie.ReadyState = READYSTATE_COMPLETE Loop Set RegEx = New RegExp MyStr = ie.Document.body.innerText Set RegMatch = RegEx.Execute(MyStr) '***************************************************************** ' Use your REGEX commands to extract data '***************************************************************** strBatchName = "F:\command.bat" Shell strBatchName next i ie.Quit Set RegEx = Nothing Set ie = Nothing end sub 

我有一个类似的问题,我find的解决scheme是:

  1. 使用John Mugginsbuild议的版本,您只需在循环结束后退出并将Internet设置为无
  2. 在每次search之间添加一个暂停,例如一秒钟,以确保它能够遍历每条logging并且毫无问题地search它们。 为了做到这一点,我build议在ie.Navigate行之前添加Application.Wait Now + 1 ,要么添加一个额外的子来做这个操作

     Sub WaitFor(PauseInSeconds As Long) Dim SngSec As Long SngSec = Timer + PauseInSeconds Do While Timer < SngSec DoEvents Loop 

    结束小组

  3. 额外:如果你有如你所说的“数百万行”,你一定要添加一个error handling程序,它检索代码崩溃的最终行,所以你不必确保它一直正常运行。 用On Error Resume Next执行此操作。 然后,您可以手动检查这些行,将它们的索引存储在一个数组中,并再次通过它们中的每一个。