简体   繁体   中英

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.

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. -

Application.SendKeys ("%lt")

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

"Executes the control identified by the idMso parameter."
Hover over the button where you would add it to the ribbon, you will see the text includes "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."

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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