VBA自动化在Internet Explorer - 单击工作不连续的网页不同的按钮?(vba automation in Internet Explorer - clicking different buttons on successive webpages not working?)

.NET Framework IT屋
问 题

I am getting an error of "object variable or with block variable not set" when this macro runs. The highlighted line to debug is the 2nd one from the bottom - "estimate.Click". When I hover the mouse over "Set estimate" on the next line up, it says "estimate=Nothing". "estimate.submit" acts the same way. The corresponding button on the webpage is never clicked. All the rest of this code is working well.

Sub btn_version()

Dim ieApp As Object
Dim ieDoc As Object
Dim ieForm As Object
Dim ieObj As Object
Dim URL As String
Dim estimate As Object

URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.navigate URL
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend

Set ieDoc = ieApp.document
Set ieForm = ieDoc.forms(1)
For Each ieObj In ieForm.Elements
If ieObj.ClassName = "AddToCartButton" Then
ieObj.Click
End If
Next ieObj

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
Set estimate = ieApp.document.getElementById("btnRequestEstimates")
estimate.Click
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub
解决方案

I'm not sure if this completely relates, but I had similar issues with JS errors using VBA automation. What I found to be successful was to make sure each and every JS function that would have been called by performing this action manually is still triggered using the code.

For example, given this button:

<BUTTON id=btnExample onfocus="return focusFunction(this);" onclick="return clickFunction(this);" name=btnExample>

VBA to call:

    Set objButton = IE.Document.getElementByID("btnExample")
    objButton.onfocus
    objButton.onclick

There may also be other functions on the page or attached to other elements that need to be called as well (which may include some sort of page unload function), so you'll have to look into the code of that window to tell for sure.

After I discovered this, following these steps has always been successful for me.

本文地址:IT屋 » vba automation in Internet Explorer - clicking different buttons on successive webpages not working?

问 题

我得到的错误“对象变量或块变量未设置”当这个宏运行。突出显示的线路调试是从底部注:第二个 - “estimate.Click”。当我将鼠标悬停在下一排队“设置估计”,它说:“估计=无”。 “estimate.submit”行为的方式相同。网页上的相应按钮从来没有点击。这code中的其他一切运作良好。

 子btn_version()昏暗ieApp作为对象
昏暗ieDoc作为对象
昏暗ieForm作为对象
昏暗ieObj作为对象
昏暗的URL作为字符串
昏暗的估计作为对象URL =“htt​​p://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx”
设置ieApp =的CreateObject(“InternetExplorer.Application”)
ieApp.Visible = TRUE
ieApp.navigate网址
虽然ieApp.Busy或者ieApp.readyState&LT;&GT; 4:调用DoEvents:WEND设置ieDoc = ieApp.document
设置ieForm = ieDoc.forms(1)
对于每个ieObj在ieForm.Elements
如果ieObj.ClassName =“AddToCartButton”然后
ieObj.Click
万一
接下来ieObj'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
虽然ieApp.Busy或者ieApp.readyState&LT;&GT; 4:调用DoEvents:WEND
设置估计= ieApp.document.getElementById(“btnRequestEstimates”)
estimate.Click
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''结束小组

解决方案

我不知道这完全涉及,但我不得不使用VBA自动化JS错误类似的问题。我认为是成功的是使该会已被调用执行此操作手动使用code仍然引发确保每一个JS的功能。

例如,给定的这个按钮:

 &LT;按钮的ID = btnExample的onfocus =“返回focusFunction(本);”的onclick =“返回clickFunction(本);”名称= btnExample&GT;

VBA来调用:

 设置objButton = IE.Document.getElementByID(“btnExample”)
    objButton.onfocus
    objButton.onclick

有也可能是页面上的其他功能或连接到需要被调用,以及(可能包括某种形式的页面卸载功能)等元素,所以你必须寻找到的code该窗口告诉是肯定的。

我发现这一点后,按照下列步骤一直是成功的我。

本文地址:IT屋 » VBA自动化在Internet Explorer - 单击工作不连续的网页不同的按钮?