Creating VBA Add-Ins To Extend and Automate Microsoft Office Documents
Creating VBA Add-Ins To Extend and Automate Microsoft Office Documents
Summary: Learn how to create add-ins for Microsoft Word, Microsoft PowerPoint, and
Microsoft Excel by using Microsoft Visual Basic for Applications (VBA). Examine when
VBA might be the best choice for your add-in and explore several sample tasks that can
be included in VBA add-ins for Microsoft Office 2010.
Last modified: April 24, 2015
Applies to: Excel 2010 | Office 2007 | Office 2010 | Open XML | PowerPoint 2010 |
SharePoint Server 2010 | VBA | Word 2010
In this article
Important
For the files referenced in these macros, you can download the Office 2010 product
guides and Interactive Menu to Ribbon guides from the Microsoft Download Center. Find a
placeholder file that you can use as the company branding guidelines document in the sample
download for this article. Note that you must save the referenced files using the path and file
names that are specified in the macros to test your add-in. If you use different path and file
names, edit the macros accordingly.
Also note that completed versions of all add-in examples in this article are included in the
sample download. However, to test the custom Help add-ins for Word, PowerPoint, and Excel,
you must download and save or install (respectively) the applicable product guides and
Interactive Menu to Ribbon guides.
VB
Option Explicit
Public oPath As String
Notice that the procedures in this code sample are declared as ribbon controls. With this
declaration in place, you can only run the procedure from an associated ribbon control. So, in
a typical add-in where the macros would have far more code, it is often more convenient to
have a separate module with a separate set of procedures for the ribbon controls and call the
macros from the controls.
Once your module is complete, you can save and close the template.
To install the global template, exit Word and save the file to the Word startup folder.
When Word starts, it automatically loads the global templates located in that folder.
Before you can load this add-in, however, you must first add the Office Open XML
markup for the custom ribbon tab, as described in the next section.
In Microsoft Windows 7 and Windows Vista, the default Word startup location is C:\Users\
[user name]\AppData\Roaming\Microsoft\Word\Startup. To manage global template add-
ins or to check if your add-in is loaded, in Word, on the Developer tab, click Add-Ins.
If you need to edit the code after you load the add-in, open the .dotm file in Word. Word
automatically replaces the previously loaded instance with the open template. Edit the
code in the Visual Basic editor, save your changes, and then close the .dotm file. The
loaded instance of the add-in automatically updates with your latest changes and
remains loaded; you do not need to restart Word.
Note
The Custom UI content in this article applies to Office 2010 but it does not apply to Office for
Mac 2011. In Word 2011, you can add custom toolbars and menus programmatically for your
specific template. But you can also do so from within Word, without using code. To do this, on
the View menu, point to Toolbars, and then click Customize Toolbars and Menus.
Also note that, as of this writing, an extra step may be needed to automatically load a global
template in Word 2011 when the application starts. If your template doesnt reload after you
following the preceding instructions, add a one-line macro in a module within the
Normal.dotm global template that instructs the macro to add. The code for this is as follows
(note that you must change the location and filename in the code to apply to your global
template):
Sub AutoExec()AddIns.Add fileName:="[Complete Path and File Name]",
Install:=TrueEnd Sub
Creating the Custom UI for Your Add-in
You might already use a utility to help you customize the ribbon in a document or
template, but adding the markup manually is just as quick and easy. As with writing any
Office Open XML markup, you can use Windows Notepad or any text editor to write the
markup. However, you might find Visual Studio useful here because it can reference the
ribbon customization schema, which means that you can take advantage of IntelliSense.
Note
In the steps that follow, the assumption is that you are opening the document package and
manually adding the customUI to your file. If you have never done that, it is a good way to
learn about the Office Open XML structure behind the document. If you have some experience
with editing an Office Open XML document package and you use Visual Studio 2010, you
might want to try the Open XML Package Editor Power Tool to open the document or
template directly in Visual Studio. Doing so saves a few steps because the tool enables you to
add folders directly to the package, makes it easier to add relationships, and automatically
manages content types when you add a part or a file. Learn more and download the Open
XML Package Editor Power Tool for Visual Studio 2010
You could also use a tool to help you build the customUI structure; for example, the Custom
UI Editor, which is free, adds the necessary parts for you, and provides samples of
customUI.xml markup.
The markup for this example includes just a few buttons, but it demonstrates a number
of customizations that you can easily include. For example, notice in Figure 1 that the
third button is a custom image. The markup also includes custom ScreenTip text and
custom KeyTip accelerators.
To add customUI markup to an Office 2010 document or template
1. Create a folder named customUI. You can create this folder in any location that
is easy for you to access, such as on your Windows desktop.
2. Add a file named customUI.xml to the new folder and then add the markup for
your ribbon customization to that file. The following code is the markup to include
for the preceding Word add-in example.
XML
Caution
If you change the file extension to open the package, dont forget to change it back when you
are done.
4. Add a relationship definition for the customUI file to the top-level relationships
(.rels) file in the ZIP package.
a. In the _rels folder, open the file .rels.
b. Add the following relationship definition. If another relationship in the file
already uses the ID rID4, select a different ID for this relationship.
XML
<Relationship Id="rID4"
Type="http://schemas.microsoft.com/office/2006/relationships/ui/ex
tensibility" Target="customUI/customUI.xml"/>
Note
This particular case requires Steps 5 through 7 because it uses a custom image for the
Company Brand Guidelines button, as shown in Figure 1. If your markup uses only built-in
images, there are no more steps required. For a list of all available built-in Office 2010 images
that you can use in your customUI markup, see the Additional Resources section at the end of
this article.
5. Create two subfolders in the customUI folder. One named _rels and, if using the
markup from this sample, the other named images.
6. Place the picture file for your custom control image (used in the third button in
this example) in the images folder and create a file named customUI.xml.rels to
place in the _rels folder. Use the following markup for the .rels file.
XML
7. Open the [Content_Types].xml file on the top level of the ZIP package. Add a
definition for the .png file extension (the file format of the custom image), if one
doesnt already exist. Use the following markup.
XML
Later in this article, see examples of additional customUI tasks, including the following:
Using a Split Button control.
Adding ribbon customization to a built-in tab.
Specifying the position for controls on a tab or for a tab on the ribbon.
Using VBA with the customUI markup to manage visibility of controls.
Creating a PowerPoint Add-in
To create a PowerPoint add-in, begin by creating a macro-enabled PowerPoint
presentation (.pptm) file. PowerPoint uses a unique file format for add-ins (.ppam), but
that format is read-only. So, you write and edit your code in the .pptm file and then save
a copy as a .ppam add-in.
To create a PowerPoint add-in
1. With the new .pptm file open, open the Visual Basic editor.
Note
If you are working in the Visual Basic editor for more than one application at a time, use
Alt+F11 to toggle between the Visual Basic Editor and the appropriate application.
2. Take the same steps as those provided in the Word add-in section of this article
for creating a new VBA project. You can then copy the code in that section for the
custom Help module, and edit it for PowerPoint as follows:
a. Replace the Documents collection object and ActiveDocument object
with Presentations and ActivePresentation, respectively.
b. Replace references to Word 2010 content and file locations with references
to comparable PowerPoint 2010 content.
The PowerPoint product guide and Interactive Menu to Ribbon guide are
also available for download from the links provided earlier. As with the
Word add-in, remember to save and name the PowerPoint sample files to
match the path and file names that are specified in the macro. Because
some of these files are different from those for the Word add-in, this will
require editing file names in the macros to match your sample file names
as well.
3. Save and close the .pptm file.
4. Add the necessary customUI folder and content to the .pptm file.
You can use the steps and markup provided in the preceding section, editing the
markup (such as labels and ScreenTip text) as needed for the selected
PowerPoint content. However, be sure to follow the steps to edit the .rels and
[Content_Types].xml files that are resident in the PowerPoint file, rather than
replacing those.
5. Open the .pptm file in PowerPoint. After confirming that the custom Ribbon tab
looks and functions correctly, save a copy of the file as a PowerPoint Add-In
(.ppam) file. PowerPoint automatically saves the add-in to the Microsoft Add-
Ins folder.
In Windows 7, the Microsoft Add-Ins default folder path is C:\Users\user
name\AppData\Roaming\Microsoft\AddIns.
Note
In PowerPoint for Mac 2011, you can programmatically add a toolbar or menu to your addin.
See the accompanying downloads for this article for a sample.
To load the PowerPoint add-in
1. In PowerPoint, on the Developer tab, click Add-Ins.
2. Click Add New.
3. Select the file that you just saved and then click Open.
4. If prompted with a macro security warning, click Enable Macros.
By default, loaded PowerPoint add-in files don't appear in the Project Explorer of the
Visual Basic editor. To view and open loaded .ppam projects, add a new value to the
Windows Registry. In the key
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\PowerPoint\Options, add
a DWORD value named DebugAddins, with a value of 1. Then, restart PowerPoint.
Note that you can view, export, and copy content from the loaded .ppam project. You
can also make edits to test them in the loaded add-in, which makes the add-in function
similar to a handy scratch pad when youre troubleshooting or want to try something
new. However, you cannot save changes directly in the loaded add-in file.
Creating an Excel Add-in
Excel add-ins might be the most efficient to create among the three programs discussed
in this article because you can save a file as an Excel add-in (.xlam), load the add-in file,
and then write and edit the code directly in the add-in file while it is loaded.
To create and load an Excel add-in
1. Create a new Excel file and save it as an Excel Add-In (.xlam). Excel
automatically saves the file to the Microsoft Add-Ins folder.
2. On the Developer tab, click Add-Ins. Your newly saved add-in should appear in
the list. If so, select it (which loads the add-in) and then close the dialog box.
Otherwise, click the Browse button to find and load your add-in file.
3. Open the Visual Basic editor. Then, create and populate the project as discussed
in the preceding Word and PowerPoint sections.
If you copy the VBA code provided in the Word add-in section, change references
to the Documents collection object and ActiveDocument object
to Workbooks and ActiveWorkbook, respectively. Also change references to
Word files and locations as appropriate for Excel content. You can download the
Excel product guide and Interactive Menu to Ribbon guide for this add-in from the
links provided earlier.
4. Exit Excel and then browse to the Microsoft Add-Ins folder to locate your new
add-in file.
5. Add the customUI to your add-in file.
If you use the markup and code provided earlier for the Word add-in, edit the
attributes (such as labels and ScreenTips) as needed for the Excel content. Be
sure to follow the preceding steps for customizing the UI to edit the .rels and
[Content_Types].xml files that are resident in the Excel file.
When you next open Excel, your add-in should still be loaded and your customUI should
now be visible.
Troubleshooting Add-ins
When your add-in does not load or run correctly, check for the following common issues:
Ribbon customization does not appear. If your ribbon customization does
not appear when the file that contains the customUI is opened (or when the add-
in is loaded), a syntax error is most likely to blame. Check the customUI.xml file
that you created and the .rels file where you added the relationship.
o Keep in mind that a very small error such as a missing quotation mark or
bracket, or incorrect capitalization can cause an XML document part to
fail.
o Also check the relationship definition that you added to the .rels file to
confirm that the path matches the name of the folder and file in which you
stored the customUI markup.
A procedure does not run. If a procedure does not run when you click an add-
in control on the ribbon, check for the following possibilities:
o In the customUI markup, does the procedure name that you used in
the OnAction attribute for that control match the name of the procedure
in your VBA code?
o In the procedure that you used as the value of the OnAction attribute, is
the customUI markup also declared as a ribbon control in VBA?
None of the add-in code runs. If the add-in UI appears in the ribbon but none
of your procedures run, the issue is most likely security.
On the Developer tab, click Macro Security. Select the option Disable all
macros with notification. Allowing all macros to run is, of course, not
advisable. But if you disable macros without notification, add-ins may be unable
to load.
What Can You Do with VBA Add-ins?
As mentioned earlier, the custom Help example here is among the simplest uses for an
add-in. You can use VBA to create add-ins that provide complete document solutions for
many types of requirements.
The tasks in the sections that follow are just a beginning. The intent is to help you get
started with concepts that you can apply to many different types of needs, including
gathering and storing user information, populating documents, evaluating document
content, and interacting with built-in tasks such as closing a file or starting the
application.
As you explore these concepts, remember that most of the underlying VBA shown can
apply to Word, PowerPoint, or Excel regardless of which application the specific example
uses.
Automating Document Creation Tasks
When automating content for users, you might need to collect and store user
information and preferences, and then use that information to generate or customize
content programatically.
In this example, a Word add-in collects contact information and paper size preference.
The add-in also gives the user the ability to generate a company-branded letter
document. When creating a letter, the add-in automatically uses the saved information.
This sample add-in contains one UserForm and three modules:
The UserForm is for collecting and storing the user information and preferences.
One module is for the variables required by multiple procedures, such as
retrieving the stored user information.
Another module is for the ribbon controls.
The last module is for the code to generate custom documents.
For this task, create a dialog box (UserForm) where the user can enter the information.
Then, save that information to the Windows Registry.
Note
Before you begin the following procedure, you must create the VBA project. Also note that,
for Office for Mac 2011, you can use the same code indicated as saving or retrieving
information in the Windows Registry. On Mac OS, these methods work the same and save
information to a .plist file in the user preferences folder of the library.
To collect and save user information
1. In the Visual Basic Editor, create and populate the UserForm with the controls
that you will need. To do so, on the Insert menu, click UserForm. If the Toolbox
(which contains the available UserForm controls) doesnt automatically appear,
on the View menu, click Toolbox.
a. Click the control that you need (such as a label or a text box) and then
drag on the UserForm to create the control with the required size.
(Alignment and spacing tools are available on the Format menu.)
b. Use the Properties window to name the control and set various
properties, such as to add a caption or an accelerator key, set height and
width, or set position from left and top. You can specify many aspects of
control appearance and behavior from this window.
Figure 2 shows the completed UserForm object for this example. Note the
usability details that make user interaction with the UserForm easy. For example,
accelerator keys are provided for keyboard users, controls are consistently sized
and aligned, and frames are used to visually organize controls.
In addition to the features that you can see, the tab order is set for ease of use
by keyboard users. To set tab order for controls in a UserForm, right-click the
form and then click Tab Order. Then, right-click in each frame and click Tab
Order to reorder the controls that appear in the selected frame.
2. Create a module for the variables that you will need for the UserForm, including
variables to retrieve the settings that the UserForm code will save and to
populate controls in the UserForm.
VB
Option Explicit
Public oName As String, oTitle As String, oAdd1 As String, oAdd2 As
String, _
oPh1 As String, oPh2 As String, oPDat1 As String, oPDat2 As String, i As
Integer, _
oPaper As String, oPhone(2) As String, oDoc As Word.Document
Sub DocVarbs()
'GetSetting and SaveSetting use VB and VBA Program Settings in the
Registry: _
HKEY_CURRENT_USER\Software\VB and VBA Program Settings
oName = GetSetting("CustomDocs", "Info", "Name")
oTitle = GetSetting("CustomDocs", "Info", "Title")
oAdd1 = GetSetting("CustomDocs", "Info", "Add1")
oAdd2 = GetSetting("CustomDocs", "Info", "Add2")
oPh1 = GetSetting("CustomDocs", "Info", "PLabel1")
oPh2 = GetSetting("CustomDocs", "Info", "PLabel2")
oPDat1 = GetSetting("CustomDocs", "Info", "PData1")
oPDat2 = GetSetting("CustomDocs", "Info", "PData2")
oPaper = GetSetting("CustomDocs", "Info", "Paper")
'sets default values for the first use of the info dialog box
'or in the case that a user generates a document with the tools
'before saving information.
If oName = "" Then
oName = Application.UserName
oChk = True
End If
If oPh1 = "" Then oPh1 = "Phone"
If oPh2 = "" Then oPh2 = "Email"
If oPaper = "" Then oPaper = "L"
End Sub
VB
Option Explicit
4. Create a module for Ribbon controls. In that module, add a procedure to display
the UserForm and declare the procedure as a Ribbon control.
VB
Notice that this example saves the user information to the Registry. That same saved
information can then be accessed by other add-ins if needed, such as when creating
custom content for PowerPoint or Excel. In fact, the code and UserForm from this
example could have been created identically in PowerPoint or Excel.
To create a custom document
For this task, start with a document template such as the company-branded
letter in this example. The code for this example generates a document from the
template. It then customizes the document based upon user preference and
populates stored user information into content controls that appear in the
document body and footer.
Notice, in Figure 3, that this letter-size document template includes two
background shapes (for the gray gradient backing and for the red bar) that will
need to be adjusted programmatically if the users paper size preference is A4.
Those shapes have been named for ease of identification in the code using
the Selection pane in Word 2010. To access the Selection pane, look on
the Home tab, and then in the Editing group, click Select.
In Figure 3, the name and title in the document body and the address, phone,
and email (as well as the phone and email labels) in the footer are in content
controls. You can insert content controls from the Developer tab, in
the Controls group. In that same group, click Design Mode to access the
placeholder text for editing. Click Properties to customize control settings, such
as to add the tags that are used to identify content controls in the code that
follows.
Note
In Word for Mac 2011, you cant create content controls or interact with them
programmatically. Instead, consider using table cells or bookmarks as reference points for
populating document content as shown in this section.
To add a bookmark so that its hidden from the user, begin the bookmark name with an
underscore. Note that you can only do this programmatically. To do so, click in the document
where you want to add the bookmark and then, in the Visual Basic Editor, use the Immediate
Window to add the bookmark. The code for adding a bookmark at the selection is shown in the
following code sample.
Selection.Range.Bookmarks.Add("_name")
Figure 3. Template that the code uses to generate a document
The code to generate and customize this document follows. It is a good idea to
create a separate module for this code because it is likely to be one of several
document types the user could create from a real world version of an add-in such as
this. Once this code is complete, add a procedure in the ribbon module created
earlier that you can use to declare a ribbon control for calling this macro.
VB
Sub NewLetter()
On Error GoTo ErrorHandler
Dim cc As ContentControl
'If the user has not yet saved their information, prompt with that
option.
If GetSetting("CustomDocs", "Info", "Name") = "" Then
i = MsgBox("Would you like to save your document details now, so that
they " _
& "can be added to your documents automatically?", vbQuestion +
vbYesNoCancel, _
"Custom Document Tools")
If i = vbYes Then
frmInfo.Show
ElseIf i = vbCancel Then
Exit Sub
End If
End If
Call modVarbs.DocVarbs
'set the defined Word.Document variable to the new document when it's
created.
'avoids errors related to multiple concurrent open documents.
Set oDoc = Documents.Add(oPath &
"\CompanyTools\CustomDocs\CompanyLetter.dotx")
'if the saved paper size preference is A4, this changes the paper size
and
'then corrects the size and position of the background graphics in the
header.
If oPaper = "A" Then
oDoc.PageSetup.PaperSize = wdPaperA4
With oDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range
With .ShapeRange("Backing")
.Width = 541.75
.Height = 841.95
End With
With .ShapeRange("Edge")
.Width = 53.2
.Height = 841.95
.Left = 542.5
End With
End With
End If
ErrorHandler:
Select Case Err.Number
Case 5174
MsgBox "The document template is unavailable." & _
" Please contact your local IT staff for assistance." _
, vbInformation, "Custom Document Tools"
Case Else
MsgBox "The task ended in error. The template may be damaged. Note "
& _
" that your saved info may not have been added to the new
document.", _
vbInformation, "Custom Document Tools"
End Select
End Sub
This example used unbound content controls for the user information. Depending
upon requirements, such as if the same user information needed to appear in
multiple locations in the document or if information appears on a cover page that
might be swapped with another cover page building block, you might prefer to use
bound controls.
Creating the UI for the Add-in
The add-in UI in this example uses a split button control placed on the Home tab,
following the Paragraph group, as shown in Figure 4. To create this Ribbon
customization, follow steps 1 through 4 in the preceding customUI example. For step 2,
use the markup that follows.
XML
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabHome" >
<group id="CustomDocs" label="Company Docs"
insertAfterMso="GroupParagraph" >
<splitButton id="CompanyDocs" size="large" >
<menu id="coDocs" >
<button id="NewLetter" visible="true" label="New Letter"
onAction="Letter" imageMso="NewPageShort"/>
<button id="UserInfo" visible="true" label="Save Info"
onAction="UserInfo" imageMso="ContactAddMyContacts"/>
</menu>
</splitButton>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
This example uses content controls for populating the saved information. But you can
take similar action to populate other objects in a Word document or in a PowerPoint
presentation or Excel workbook. For example, populate content in a table cell or at a
bookmarked location in a Word document; in a text box in PowerPoint; or in a worksheet
cell in Excel.
Note
Application events, discussed in the following section, are not widely supported in Office for
Mac 2011. PowerPoint 2011 doesnt support Application Events, and Word 2011 has very
limited support. However, you can use the same steps for setting up the module and class
module in Excel for Mac 2011 and see the range of application events available there. See the
Note that follows the procedure in the next section for help getting started with application
events in Word or Excel. Also note that document-level events are supported in Word 2011 and
that PowerPoint 2011 supports a selection of slideshow-specific macros.
Interacting with Application Events
There might be many reasons why you want your add-in to interact with built-in actions
that occur in the application or in a particular document, such as when the user prints or
closes a file. VBA gives you a few ways to interact with events, including those that
occur at the application-level (that is, regardless of which document is open) and those
that occur at the document-level (that is, only apply to a specific document, such as
those based on a particular template).
Document-level events are more commonly included in VBA for an individual document
or template. And you might create code in your add-in to interact with the behavior that
is built in to the document or template. But, application-level events are often more
convenient for add-ins because they can be built directly into your Word, PowerPoint, or
Excel add-in to interact with actions that the user takes.
This example examines how to create an application-level event in PowerPoint to check
company presentations when the user closes the file. The code checks for multiple
masters in the presentation and gives the user the option to programmatically clean-up
the masters and leave only the original, properly-branded master in the file.
Note
The article uses this example because it helps to address a common user complaint,
particularly in companies migrating from a version of Microsoft Office that uses the legacy
file formats (Office 2003 or earlier) to Office 2010 (or Office 2007). When users copy slides
from presentations that were created in an earlier version, they often use the Keep Source
Formatting command because it appears to make the slide look like the others in the
presentation. But, that command actually adds another master and set of layouts every time
that it is used. The results are presentations with an unwieldy number of masters, inconsistent
formatting, and bloated file size that users and tech support staff interpret as damaged or
corrupted files. A tool that is similar to the example code can be used to help the user before
presentations become difficult to manage and to educate the user about the ramifications of
specific actions.
To create an application event, you must add some code to any existing module in your
VBA project, and add a class module for the event code itself. The following example
also uses a UserForm so that it can take action that is based on user preference.
To create an application-level event in your add-in
1. On the Insert tab, click Class Module to create a class module. In
the Properties window, give the class module an intuitive name, such
as EventClassModule.
2. Add code to an existing module in your project to instantiate the new class
module. This requires just one declaration (note the use of the class module
name in the Dim statement that appears at the top of the module) and two small
procedures.
VB
Sub InitializeMSTApp()
Set XMST.app = Application
End Sub
3. Create the UserForm that will query the user whether they want the masters
removed from the presentation. This example uses a UserForm instead of a
simple message box in order to give the user an additional option.
Use the tips in the preceding section to create a UserForm. The finished UserForm
has one label, two command buttons, and one check box, as shown in Figure 5.
VB
5. To add the code for the event, at the top of the class module, declare an
application variable for use with events. Then, add the procedure. In this case,
the PresentationClose event is the procedure used.
Notice that the following code uses two additional
variables, oCncl as Boolean and i as Integer. To publicly declare these variables
for use throughout the project, declare them at the top of any regular module.
VB
After you save this file as an add-in and load it in PowerPoint, you need a presentation
that contains the CompanyPres variable and more than one master to test your add-in.
Use the sample document that is included in the download for this article; in addition,
use the template to create your own test presentations.
Note
You can use the preceding steps 1 and 2 in Word or Excel as well to setup a class module for
application events. The code in step 2 is identical for any of the programs and you can use the
same statement Public WithEvents app As Application at the top of the class module in
the Visual Basic Editor for Word or Excel to declare a variable for working with application
events. After you do, select app from Object list (the dropdown list to the left above the code
window) and you will then see available events in the Procedure list (the dropdown list to the
right above the code window).
Controlling the Add-in UI Programmatically
When you create a custom UI for your add-in, you might occasionally need more control
over the appearance of the UI than Office Open XML markup alone can provide. For
example, label text for a control might vary based on specific conditions, or visibility of
controls might vary based on user preferences. Fortunately, the customUI schema
includes a selection of attributes that give you this type of flexibility.
In the following scenario, a company has a set of custom tools for Excel on the ribbon.
Some users want to see the tab and other users do not. The add-in queries the user on
startup whether to show or hide the custom tab. If the user chooses to hide the custom
tab, the add-in provides a control at the end of the Home tab where the user can
change the way the custom tab is displayed.
This VBA project requires a module in which to put the ribbon control procedures, a
UserForm for retrieving the users preference, and code in the ThisWorkbook object of
the add-in where you can create the event that runs the code whenever the user starts
Excel.
The procedure that follows requires that you already have the customUI.xml markup for
both the custom ribbon tab and for the reset control that will appear for users who
initially do not want the tab.
To control ribbon customization behavior programmatically
1. Create the UserForm that will prompt users for their preference when they start
Excel.
As in the preceding section, this example uses a UserForm instead of a message
box in order to give the user an additional option.
VB
3. In the ThisWorkbook object for the project, add a Workbook_Open event. The
code shown here displays the dialog box if the user has not already indicated
that they never want to be prompted. It then shows the dialog box and saves the
users newly-selected preference (Yes, No, or Never) to the registry.
VB
VB
5. In the customUI.xml file, add the GetVisible attribute to the tag for your custom
tab and to the tag for the group in which the reset button will appear. Use the
procedure name ShowControls as the value for both.
Following first is the markup just for the two edited lines. The second set of
markup shows these lines in context.
XML
<tab id="TabCompany" label="Company Tools" insertBeforeMso="TabHome"
getVisible="ShowControls">
<group id="CustomTools" label="Company Options"
getVisible="ShowControls">
Note that the custom tab in the next example uses an arbitrary selection of built-
in commands since the purpose of this example is to show the visibility control
for the tab itself.
XML
https://msdn.microsoft.com/en-us/library/office/gg597509(v=office.14).aspx