[英]How to programmatically click an ActiveX button in another workbook?
I have a workbook Wbk1
where a sheet has an ActiveX button. 我有一个工作簿
Wbk1
,其中工作表具有ActiveX按钮。 I want to run the button's associated Sub from another workbook, Wbk2
. 我想从另一个工作簿
Wbk2
运行按钮的关联Sub。 It's not practical to just copy the Sub's code into Wbk2
because it in turn calls a bunch of functions from Wbk1
. 仅将Sub的代码复制到
Wbk2
是不实际的,因为它反过来又从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; 这不仅是一个笨拙的
SendKeys()
骇客,而且还是行不通的。 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.
我知道For循环正确地遍历了对象(包括CommandButton),因为我可以选择按钮的标题。 How can I click on this button (and thereby run its private Sub) from
Wbk2
? 如何从
Wbk2
单击此按钮(从而运行其私有Sub)?
EDIT: The actual filename in question is in the format 123A_1.0-2.0.xlsm. 编辑:有问题的实际文件名的格式为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.
我认为这些时间段会给下面评论和回复中发布的解决方案带来一些麻烦,因为当我删除这些时间段时,这些技术是成功的。
怎么样
Application.Run (wb1.Name & "!MacroName")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.