[英]Call Excel Macro from Ribbon Button
I have a VBA Macro that works perfect in excel via a button, but i want to include a button in the excel ribbon(which i have done) but i do not know how to connect the ribbon button to the macro correctly, here is my C# and XML code: 我有一个VBA宏,可以通过按钮在excel中完美工作,但是我想在excel功能区中包含一个按钮(我已经完成了),但是我不知道如何将功能区按钮正确连接到宏,这是我的C#和XML代码:
C# C#
public class Ribbon1 : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
TimeSpan startTimeSpan = new TimeSpan(0, 0, 5, 0);
TimeSpan timeDecrease = TimeSpan.FromSeconds(1);
private Timer timer = new Timer();
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
timer.Interval = 1000;
this.ribbon = ribbonUI;
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if (startTimeSpan.ToString() != "00:00:00")
{
startTimeSpan = startTimeSpan - timeDecrease;
ribbon.InvalidateControl("timerLabel");
}
else
{
startTimeSpan = new TimeSpan(0, 0, 5, 0);
return;
}
}
public string timerLabel_getLabel(Office.IRibbonControl control)
{
return startTimeSpan.ToString();
}
private void button1_onAction(Office.IRibbonControl control)
{
Globals.ThisWorkbook.Application.Run("'PM MailMerge.xlsm'!MailMerge.MailMerge");
}
XML XML
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui" >
<ribbon>
<tabs>
<tab id="TimerTest"
label="Practice Monitoring">
<group id="group1"
label="MailMerge">
<labelControl id="timerLabel"
getLabel="timerLabel_getLabel"/>
<button id="button1"
label="Merge"
size="large"
onAction="'PM MailMerge.xlsm'!MailMerge.MailMerge"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
这将从当前打开的excel工作簿运行宏,或者从xlstart(C:\\ Users \\\\ AppData \\ Roaming \\ Microsoft \\ Excel \\ XLSTART)文件夹中提供的工作簿运行宏
Globals.ThisWorkbook.Application.Run("Your macroname")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.