HelenT
HelenT

Reputation: 51

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

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.

Upvotes: 1

Views: 2811

Answers (1)

Olle Sjögren
Olle Sjögren

Reputation: 5385

  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.

Upvotes: 2

Related Questions