从功能区按钮调用 Excel 宏 [英] Call Excel Macro from Ribbon Button

查看:87
本文介绍了从功能区按钮调用 Excel 宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VBA 宏,它通过一个按钮在 excel 中完美运行,但我想在 excel 功能区中包含一个按钮(我已经完成了),但我不知道如何将功能区按钮正确连接到宏,这是我的 C# 和 XML 代码:

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:

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 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) 文件夹

This will run the macro from the current opened excel workbook or macro from the workbook available in xlstart(C:\Users\\AppData\Roaming\Microsoft\Excel\XLSTART) folder

 Globals.ThisWorkbook.Application.Run("Your macroname")

这篇关于从功能区按钮调用 Excel 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆