简体   繁体   English

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

[英]Excel Internet Explorer Automation Causes IE Environment to Crash

I've successfully automated a VBA Excel macro to iterate through a loop and hit a series of URL's to trigger a server-side script - this is simply done with: 我已经成功地使VBA Excel宏自动化,以遍历循环并命中一系列URL来触发服务器端脚本-只需完成以下操作即可:

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

where var_string is assigned within the loop as it iterates through. 遍历循环时在循环内分配var_string位置。 Before this, I've cleared cache, cookies and history with: 在此之前,我已经使用以下方法清除了缓存,Cookie和历史记录:

Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess #

I've tried many #'s including 8,2, 16 etc to see if any of these had an effect (and combination of #'s). 我尝试了许多#,包括8,2、16等,以查看其中的任何一个是否有效果(以及#的组合)。

The issue I am having, is that although the entire script SOMETIMES works, if I were to run it a second time the line where I navigate to the URL fails to call the URL even though the domain/URL is fully live and functional. 我遇到的问题是,尽管整个脚本SOMETIMES都能正常工作,但如果我第二次运行它,即使该域/ URL完全可用并且可以正常运行,我导航到该URL的行也无法调用该URL。 Any other URL I manually type into the IE window works just fine - just not the one I am calling inside the loop. 我手动在IE窗口中键入的任何其他URL都可以正常工作-只是我在循环内调用的URL不起作用。 IE is locking me out of that domain temporarily. IE暂时将我锁定在该域之外。 If I come back to the script a few hours from last running it, it generally works. 如果我在上次运行该脚本后几小时返回该脚本,则通常可以正常运行。

Again the domain is functional and script is fine - I verify it all the time with another machine. 同样,该域可以正常工作并且脚本很好-我一直在另一台计算机上对其进行验证。

It's as if I am setting something environmentally and breaking Internet Explorer within VBA even though the script is absurdly simple. 就好像我在环境中设置某些东西并在VBA中破坏Internet Explorer一样,尽管脚本非常简单。

I've tried CreateObject() , GetObject as well as InternetExplorerMedium for myIE object. 我已经为myIE对象尝试过CreateObject()GetObject以及InternetExplorerMedium

If all you need is to "touch" that URL for its side effects, you can as well use a XMLHTTP object. 如果您需要的只是“触摸”该URL的副作用,那么您也可以使用XMLHTTP对象。 In VBA, go to menu Tools, then References and choose Microsoft XML, v6.0. 在VBA中,转到菜单工具,然后选择引用,然后选择Microsoft XML v6.0。 Then: 然后:

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

Some notes: 一些注意事项:

  • You may want to use POST instead of GET, if you're having problems with caching 如果您在缓存方面遇到问题,则可能要使用POST而不是GET
  • You should pass the data in the POST body, if the server can handle it 如果服务器可以处理,则应在POST正文中传递数据
  • The value of var_string should be escaped, in this case, URL encoded var_string的值应转义,在这种情况下,应使用网址编码
  • If you don't want to block waiting for responses, you can make requests asynchronously ( True third argument to open ) 如果您不想阻塞等待响应,则可以异步发出请求( Trueopen第三个参数)

Following these notes, here's a more elaborate example: 根据这些说明,下面是一个更详细的示例:

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

For the missing pieces, here's the code for CXMLHTTPHandler , which I actually found through stackoverflow , and a definition of UrlEncode at stackoverflow . 对于缺少的部分,这是CXMLHTTPHandler的代码 ,我实际上是通过stackoverflow找到的,以及在stackoverflow定义的UrlEncode

You should specialize CXMLHTTPHandler for your needs, probably even make it accept an AddressOf a procedure and call it in the actual default procedure. 您应该根据需要专门化CXMLHTTPHandler ,甚至可能使它接受一个AddressOf过程并在实际的默认过程中调用它。 The default procedure should set the m_xmlHttp to Nothing when m_xmlHttp.readyState is 4 . m_xmlHttp.readyState4时,默认过程应将m_xmlHttp设置为Nothing


EDIT 1: If your request code is in a loop, you need to break the Dim ... New statements in two, to ensure you're using fresh objects: 编辑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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM