简体   繁体   English

如何以编程方式单击另一个工作簿中的ActiveX按钮?

[英]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.

相关问题 ISO如何对工作簿1中的ActiveX按钮进行编程,以在工作簿2中运行宏? - ISO How to program ActiveX button in Workbook 1, to run macros in Workbook 2? 如何通过单击按钮从另一个工作簿自动编号? - How to automatic number from another workbook at a click of a button? 如何让 VBA 单击不同工作簿中的按钮? - How to make VBA click a button in a different workbook? 如何在另一个工作簿上的消息框上单击确定 - How to click ok on a msgbox on another workbook ActiveX Multiselect Listbox适用于一个Excel工作簿,但不适用于另一工作簿 - ActiveX Multiselect Listbox working for one excel workbook but not working in another workbook 单击按钮后使用宏将数据保存到另一个Excel工作簿 - Save data to another excel workbook after button click using macro 如何将代码导入另一页上的Activex控件按钮? - How can I import code to a Activex Control Button on another page? Excel VBA代码适用于个人宏工作簿,但不适用于ActiveX按钮 - Excel VBA code works in Personal Macro Workbook but not as ActiveX button Excel VBA - 使用来自另一个工作簿的动态范围填充 ActiveX 组合框 - Excel VBA - Fill ActiveX Combobox with dynamic range from another workbook 如何通过单击按钮将工作簿中的选项卡导出到单个 Excel 工作簿 - How to export tabs in a workbook to individual excel workbooks with button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM