Word 2010: How do I attach a ribbon button to a template using VBA

淺唱寂寞╮ 提交于 2019-12-11 21:27:48

问题


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.

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:


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

  2. 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>
    
  3. 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
    
  4. 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.



来源:https://stackoverflow.com/questions/32988202/word-2010-how-do-i-attach-a-ribbon-button-to-a-template-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!