Word 2010:如何使用VBA将功能区按钮附加到模板 [英] Word 2010: How do I attach a ribbon button to a template using VBA

查看:159
本文介绍了Word 2010:如何使用VBA将功能区按钮附加到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Word 2010上的自定义功能区按钮分配一个宏,但只能用于特定的模板文件,以便仅当该模板已附加到我正在处理的文件上时,该按钮才会出现.

I want to assign a macro to a custom ribbon button on Word 2010 BUT ONLY FOR A PARTICULAR TEMPLATE FILE so that the button only appears when that template has been attached to the file I'm working on.

在我的办公室中,将有大约30个用户通过网络使用此按钮,因此,想法是将按钮存储在模板文件中将使该按钮可移植到其他用户,而无需我手动将其安装在每个用户的PC.

There will be about 30 users using this over the network in my office, so the idea is that storing the button on the template file will allow the button to be portable to other users without me having to manually install it on each user's PC.

推荐答案

  1. 创建一个启用宏的模板文件(扩展名为.dotm).

  1. Create a macro-enabled template file (with the .dotm extension).

使用

Add the XML for the ribbon button you want to the template with the Custom UI Editor and assign a callback function:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" >
    <ribbon startFromScratch="false" >
        <tabs>
            <tab id="myTab1" label="Tab #1">
                <group id="myGrp1" label="Group #1">
                    <button id="myBtn1" label="Button #1" imageMso="HappyFace" size="large" onAction="Callback" />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

  • 在模板文件中添加回调函数的代码:

  • Add the code for the callback function in the template file:

    Option Explicit
    
    'Callback for button onAction
    Sub Callback(control As IRibbonControl)
        MsgBox "Gentlemen, we have a macro!"
    End Sub
    

  • 基于模板创建一个新文档.

  • Create a new document based on the template.

    即使未打开模板文件,您也应有权访问按钮和宏.

    You should have access to the button and macro even if the template file is not opened.

    这篇关于Word 2010:如何使用VBA将功能区按钮附加到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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