Sheldon
Sheldon

Reputation: 11

How do I click a button on a webpage through VBA?

I am trying to use click a button through vba but it tells me that the "Object doesn't support this property or method".

My VBA code right now is:

IE.document.getElementByType("button").getelementByClass("qm_historyTab_GoButton").Click

The HTML for the button is:

<button class="qm_historyTab_GoButton" type="submit" style="font:normal 11px tahoma,arial,helvetica,sans serif;">
  <span class="qm_historyTab_GoButtonText">GO</span>
</button>

Upvotes: 0

Views: 477

Answers (2)

Sheldon
Sheldon

Reputation: 11

Ok so I found the solution in this specific instance. The button is a submit type and sends a message using HTMLInputElement.

What I needed to do was add "Microsoft HTML Object Library" to my references and utilize those features.

I had to create the variable that would send the response:

Dim htmlInput As MSHTML.HTMLInputElement

I couldn't figure out how to refer to this specific element individually so I grabbed everything with it's class name.

For Each htmlInput In IE.document.getElementsByClassName("qm_historyTab_GoButton") If Trim(htmlInput.Type) = "submit" Then htmlInput.Click Exit For End If Next htmlInput

Then I had the program cycle through each item in the series and send a submit response. Since this was the only one with the class name, I got lucky.

So anyways, that's how I solved the problem.

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166126

Try this:

IE.document.getElementsByTagName("button") _
   .getelementsByClassName("qm_historyTab_GoButton")(0).Click

Both of the getXXX methods return a collection of items (even if there's only one match in the document) so to address a single item from the last one in order to click it you need to add the (0)

EDIT: if the aim is to submit the form then this will also work

IE.document.getElementById("qm_historyForm_7871").submit

or as there's only one button with that classname:

IE.document.getElementsByClassName("qm_historyTab_GoButton")(0).Click

Upvotes: 1

Related Questions