I have a workbook Wbk1
where a sheet has an ActiveX button. I want to run the button's associated Sub from another workbook,Wbk2
. It's not practical to just copy the Sub's code into Wbk2
because it in turn calls a bunch of functions from Wbk1
. I tried the following:
Sub pushButton()
Dim obj As OLEObject
Dim btn As MSForms.CommandButton
For Each obj In Wkb1.Sheets("testSheet").OLEObjects
If TypeOf obj.Object Is MSForms.CommandButton Then
set btn=obj.Object
debug.print btn.Caption 'used to test whether or not For loop is picking up button
obj.Activate
SendKeys " "
End If
Next
End Sub
It's not only an inelegant SendKeys()
hack, but it doesn't work; the button gets focus but it doesn't get pushed. I know that the For loop is correctly iterating through the objects, including the CommandButton, because I can pick up the caption for the button. How can I click on this button (and thereby run its private Sub) from Wbk2
?
EDIT: The actual filename in question is in the format 123A_1.0-2.0.xlsm. I think the periods are causing some trouble with regard to the solutions posted in comments and responses below, because when I remove the periods these techniques are successful.