簡體   English   中英

通過宏(VBA)以編程方式單擊 Excel 中的按鈕

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

我有一個自定義按鈕,鏈接到一個做一些事情的宏。 完成后,我希望代碼以編程方式單擊下面突出顯示的按鈕(更清楚的是,打開導入 XML 對話框),因此我的用戶可以看到文件選擇 window 並且它將繼續執行它的進一步任務。

代表形象

在此處輸入圖像描述

這是我當前的宏代碼:

Option Explicit

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

我應該在宏中添加什么來實現這一點?

我找到了我的問題的解決方案-

% - Alt
l - Developer
t - Import

是將 XML 導入工作表的快捷鍵,此命令以編程方式運行快捷鍵,打開我想要的文件選擇框。 -

Application.SendKeys ("%lt")

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

“執行由idMso參數標識的控制。”
Hover 在您將其添加到功能區的按鈕上,您將看到文本包括“XmlImport”

“這種方法在沒有 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