简体   繁体   English

通过宏(VBA)以编程方式单击 Excel 中的按钮

[英]Programmatically click a button in Excel through Macro (VBA)

I have a custom-button, linked to a macro which does a few things.我有一个自定义按钮,链接到一个做一些事情的宏。 After they are done, I want the code to programmatically click the below highlighted button (more clearly, open the import XML dialog), so my user could see the file selection window and it would proceed for it's further tasks.完成后,我希望代码以编程方式单击下面突出显示的按钮(更清楚的是,打开导入 XML 对话框),因此我的用户可以看到文件选择 window 并且它将继续执行它的进一步任务。

Representative image代表形象

在此处输入图像描述

Here's my current macro code:这是我当前的宏代码:

Option Explicit

Sub Button1_Click()
    ' ... All my existing macro code
    
    ' ---------- programmatically click the Import button (from Developer) here ------------
End Sub

What should I add in my macro to achieve this?我应该在宏中添加什么来实现这一点?

I have found the solution to my question -我找到了我的问题的解决方案-

% - Alt
l - Developer
t - Import

are the shortcut keys to import an XML into the sheet, and this command runs the shortcut keys programmatically, opening my desired file selection box.是将 XML 导入工作表的快捷键,此命令以编程方式运行快捷键,打开我想要的文件选择框。 - -

Application.SendKeys ("%lt")

CommandBars.ExecuteMso method: CommandBars.ExecuteMso 方法:
https://docs.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso https://docs.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso

"Executes the control identified by the idMso parameter." “执行由idMso参数标识的控制。”
Hover over the button where you would add it to the ribbon, you will see the text includes "XmlImport" Hover 在您将其添加到功能区的按钮上,您将看到文本包括“XmlImport”

"This method is useful in cases where there is no object model for a particular command. Works on controls that are built-in buttons, toggleButtons, and splitButtons." “这种方法在没有 object model 用于特定命令的情况下很有用。适用于内置按钮、切换按钮和拆分按钮的控件。”

Private Sub ExecuteMso_MoveToFolder()
    ' https://docs.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso
    ' Hover over the button where you would add it to the ribbon,
    '  you will see the text includes "XmlImport"

    CommandBars.ExecuteMso ("XmlImport")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM