0
votes

Just need to have a better understanding of using the internet explorer object in VBA. So I want to click the following button with a code as following

<a id="DLG_VARIABLE_dlgBase_BTNOK" ct="B" st="" href="javascript:void(0);" class="urBtnEmph" ocl="sapbi_page.sendCommandArray([['TARGET_DIALOG_REF','DLG_VARIABLE',0],['BI_COMMAND_TYPE','OK',0]],event);" onkeydown="ur_Button_keypress(event);" onclick="ur_Button_click(event);" style="text-align:center;overflow:visible;">OK</a>

My code is as following -

 Dim IE As InternetExplorer 'Reference to Microsoft Internet Controls
 Set IE = New InternetExplorer
 With IE
 .Visible = True
 .Navigate2 "Somewebsite"

 .Document.getelemetbyclass("urBtnEmph").Click

 End With

End Sub

but when I try to run it, it says

'Object doesn't support this property or method'.

I'm very new to this internet explorer object and javascript :(

Edit: I had changed the line to ' .Document.getelementsbyclass("urBtnEmph").Click' however still have the same warning.

Then I tried this '.Document.getElementById("DLG_VARIABLE_dlgBase_BTNOK").Click' and it shows 'Object variable or with block variable not set'. I had attached the html code for this button

enter image description here

1
Hi brax, I had changed to .Document.getElementsByClass("urBtnEmph").Click but still shows the same problem. Did I choose the wrong class name?Emmi Zhao
It's getElementsByClassName but since it has an id you should target that. You're not waiting for the page to load, so you will be looking for the element before it's even rendered. If you fix that then .Document.getElementById("DLG_VARIABLE_dlgBase_BTNOK").Click should work.Tim Williams
On the screenshot it looks like the document you want to access is located in an iFrame. But you only see the closing iFrame tag. Show as much of the HTML code that the opening part is also visible. Preferably not as a screenshot, but as a code snippetZwenn

1 Answers

0
votes

Please check the Common VBA Methods & Properties used in web automation, you could use the getElementsByClassName or getElementById method to access web page element.

When using the getElementsByClassName method, it will return multiple elements with the same class name, so, if we want to get the special element, we could find it from the array list.

So, please modify your code as below:

Public Sub ClickTest()

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend 

        'ie.Document.getElementbyId("DLG_VARIABLE_dlgBase_BTNOK").Click  // it also work well. find element by id 

        ie.Document.getElementsByClassName("urBtnEmph")(0).Click

    End With
End Sub