cydan
cydan

Reputation: 625

VBA macro that clicks all the buttons

I'm trying to make a macro that clicks all the buttons in any document by getting all the buttons names and evaluating a call to these functions.

Code:

Private Sub CommandButton1_Click()
MsgBox "b"
End Sub

Private Sub CommandButton2_Click()
MsgBox "a"
End Sub

Sub test_macro()
For Each S In Worksheets(1).OLEObjects
    Evaluate ("Call " + S.Name + "_Click")
Next
End Sub

What can be the problem here? and is there maybe another way to do it?

Upvotes: 2

Views: 285

Answers (1)

Hao Zhang
Hao Zhang

Reputation: 211

See if this works.

For Each OLEObject In Worksheets(1).OLEObjects
    If TypeName(OLEObject.Object) = "CommandButton" Then OLEObject.Object = True
Next

Here are some additional documentations on OLEObject.

Upvotes: 2

Related Questions