Excel Internet Explorer自动导致IE环境崩溃

我已经成功地自动化了一个VBA Excelmacros来遍历一个循环,然后点击一系列的URL来触发一个服务器端脚本 – 这只需要完成:

myIE.Navigate ("http://someURL.php?VARIABLE=" & var_string) 

var_string在循环中被赋值。 在此之前,我已经清除了caching,Cookie和历史logging:

 Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess # 

我已经尝试了许多#包括8,2,16等,看看这些是否有效果(和#的组合)。

我遇到的问题是,尽pipe整个脚本SOMETIMES都起作用,但是如果我要再次运行该脚本,则导航到该URL的行将无法调用URL,即使该域/ URL是完全有效且function正常的。 任何其他的URL我手动input到IE窗口工作得很好 – 只是不是我在循环内呼叫。 IE正在locking我暂时离开该域名。 如果我从上次运行数小时后回到脚本,它通常工作。

这个域又是function性的,脚本很好 – 我用另外一台机器来validation它。

就好像我正在设置一些环境并打破VBA内的Internet Explorer,即使脚本非常简单。

我已经尝试过CreateObject()GetObject以及InternetExplorerMedium for myIE对象。

如果您只需要“触摸”该URL的副作用,则还可以使用XMLHTTP对象。 在VBA中,进入菜单工具,然后select参考,然后selectMicrosoft XML,v6.0。 然后:

 Dim Request As New XMLHTTP Request.open "GET", Url & "?VARIABLE=" & var_string, False Request.send ' Check Request.status, probably for 200 

一些说明:

  • 如果您遇到caching问题,您可能需要使用POST而不是GET
  • 如果服务器可以处理它,你应该传递POST正文中的数据
  • var_string的值应该被转义,在这种情况下,URL编码
  • 如果你不想阻止等待响应,你可以asynchronous地发出请求( True第三个参数open

以下是这些笔记,下面是一个更详细的例子:

 Dim Request As New XMLHTTP Request.open "POST", Url, True Dim Handler As New CXMLHTTPHandler Handler.Initialize Request Set Request.onreadystatechange = Handler Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" Request.send UrlEncode("VARIABLE") & "=" & UrlEncode(var_string) ' This returns immediately, the check must now be done in the Handler 

对于缺less的部分,下面是通过stackoverflowfind的CXMLHTTPHandler的代码 ,以及在stackoverflow中定义的UrlEncode 。

您应该专门为您的需要CXMLHTTPHandler ,甚至可能使其接受AddressOf过程,并在实际的默认过程中调用它。 当m_xmlHttp.readyState4时,默认过程应该将m_xmlHttp设置为Nothing


编辑1:如果你的请求代码是在一个循环中,你需要打破Dim ... New语句在两个,以确保您使用新的对象:

 Dim Request As XMLHTTP Set Request = New XMLHTTP Request.open "POST", Url, True Dim Handler As CXMLHTTPHandler Set Handler = New CXMLHTTPHandler Handler.Initialize Request Set Request.onreadystatechange = Handler Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" Request.send UrlEncode("VARIABLE") & "=" & UrlEncode(var_string) ' This returns immediately, the check must now be done in the Handler