vba automation in Internet Explorer - clicking different buttons on successive webpages not working? [duplicate]

有些话、适合烂在心里 提交于 2019-12-12 01:52:08

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/8826097/vba-automation-in-internet-explorer-clicking-different-buttons-on-successive-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!