[英]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: 一些注意事项:
var_string
should be escaped, in this case, URL encoded var_string
的值应转义,在这种情况下,应使用网址编码 True
third argument to open
) True
为open
第三个参数) 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.readyState
为4
时,默认过程应将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.