Chapter 26

Creating Excel add-ins

In this chapter, you will:

  • Learn what a standard add-in is

  • Learn how to create, install, and uninstall an add-in

  • Use a hidden workbook as an alternative to an add-in

You can create standard add-in files for your clients to use by employing VBA. After the client installs your add-in on her PC, the program will be available to Excel and will load automatically every time she opens Excel. This chapter discusses standard add-ins.

Be aware that there are two other kinds of add-ins: COM add-ins and Office add-ins. Neither of these can be created with VBA. You need either Visual Basic.NET or Visual C++ to create COM add-ins. You use HTML, CSS, and JavaScript to create Office add-ins. Chapter 27, “An introduction to creating Office add-ins,” familiarizes you with the basics of creating Office add-ins.

Characteristics of standard add-ins

If you are going to distribute an application, you might want to package the application as an add-in. Typically saved with an .xlam extension, an add-in offers several advantages:

  • Usually, clients can bypass your Workbook_Open code by holding down the Shift key while opening the workbook. With an add-in, they cannot bypass the Workbook_Open code in this manner.

  • After you use the Add-ins dialog box to install an add-in (by selecting File, Options, Add-Ins, Manage Excel Add-Ins, Go), the add-in will always be loaded and available.

  • Programs in an installed add-in can still run even if the macro security level is set to disallow macros.

  • Generally, custom functions work only in the workbook in which they are defined. A custom function added to an add-in is available to all open workbooks.

  • The add-in does not show up in the list of open files in the Window menu item. The client cannot unhide the workbook by choosing View, Window, Unhide.

images Caution

There is one strange rule for which you need to plan. An add-in is a hidden workbook. Because the add-in can never be displayed, your code cannot select or activate any cells in the add-in workbook. You are allowed to save data in your add-in file, but you cannot select any part of the file. Also, if you do write to your add-in file data that you want to be available in the future, your add-in code needs to handle saving the file. Because your clients will not realize that the add-in is there, they will never be reminded or asked to save an unsaved add-in. You might, therefore, add ThisWorkbook.Save to the add-in’s Workbook_ BeforeClose event.

Converting an Excel workbook to an add-in

Add-ins are typically managed using the Add-Ins dialog box. This dialog box presents an add-in name and description, which you control by entering two specific properties for the file before you convert it to an add-in.

images Note

If you’re modifying an existing add-in, you must make it visible before you can edit the properties. See the section “Using the VB Editor to convert a file to an add-in” later in this chapter.

To change the title and description shown in the Add-Ins dialog box, follow these steps:

  1. Select File, Info. Excel displays the Document Properties pane on the right side of the window.

  2. From the Properties drop-down menu, select Advanced Properties.

  3. Enter the name for the add-in in the Title field.

  4. Enter a short description of the add-in in the Comments field (see Figure 26-1).

  5. Click OK to save your changes.

  6. Click the back arrow at the top left of the screen to return to your workbook.

There are two ways to convert a file to an add-in. The first method, using Save As, is easier but has an annoying by-product. The second method uses the VB Editor and requires two steps, but it gives you some extra control. The sections that follow describe the steps for using these methods.

The figure shows the Properties dialog box on an add-in.

FIGURE 26-1 Fill in the Title and Comments fields before converting a workbook to an add-in.

Using Save As to convert a file to an add-in

Select File, Save As, Browse. In the Save As Type field, scroll through the list and select Excel Add-In (*.xlam).

As shown in Figure 26-2, the file name changes from filename.xlsm to filename.xlam. Also note that the save location automatically changes to an AddIns folder. The location of this folder varies by operating system, but it will be something along the lines of C:UsersusernameAppDataRoamingMicrosoftAddIns. It is also confusing that, after the .xlsm file is saved as an .xlam type, the unsaved .xlsm file remains open. It is not necessary to keep an .xlsm version of the file because it is easy to change an .xlam back to an .xlsm for editing.

The figure shows the Save As dialog box. The Save As Type is set for add-ins. The file name is updated to have an .xlam extension and the save to folder is now the AddIns folder.

FIGURE 26-2 The Save As method changes the IsAddin property, changes the name, and automatically saves the file in your AddIns folder.

images Tip

If, before selecting the add-in file type, you are already in the folder to which you want to save, just click the back arrow in the Save As window to return to that folder.

images Caution

When the Save As method is being used to create an add-in, a worksheet must be the active sheet. The add-in file type is not available if a chart sheet is the active sheet.

Using the VB Editor to convert a file to an add-in

The Save As method is great if you are creating an add-in for your own use. However, if you are creating an add-in for a client, you probably want to keep the add-in stored in a folder with all the client’s application files. It is fairly easy to bypass the Save As method and create an add-in using the VB Editor:

  1. Open the workbook that you want to convert to an add-in.

  2. Switch to the VB Editor.

  3. In the Project Explorer, click ThisWorkbook.

  4. In the Properties window, find the property called IsAddin and change its value to True, as shown in Figure 26-3.

    The figure shows the ThisWorkbook module's Properties window. The IsAddin property is highlighted, and its value is set to True.

    FIGURE 26-3 Creating an add-in is as simple as changing the IsAddin property of ThisWorkbook.

    Press Ctrl+G to display the Immediate window.

  5. In the Immediate window, save the file, using an .xlam extension, like this:

    ThisWorkbook.SaveAs FileName:="C:ClientFilesChap26.xlam", _
    FileFormat:= xlOpenXMLAddIn

You’ve now successfully created an add-in in the client folder that you can easily find and email to your client.

images Tip

If you ever need to make an add-in visible—for example, to change the properties or view data you have on sheets—repeat the previous steps except select False for the IsAddin property. The add-in becomes visible in Excel. When you are done with your changes, change the property back to True.

Having a client install an add-in

When you email an add-in to a client, have her save it on her desktop or in another easy-to-find folder. You should tell her to follow these steps:

  1. Open Excel and select File, Options. The Excel Options dialog appears.

  2. In the left navigation pane, select Add-Ins.

  3. At the bottom of the window, select Excel Add-Ins from the Manage drop-down menu (see Figure 26-4).

    A screenshot of the Add-Ins options in the Excel Options dialog box. The Manage drop-down menu is open and the Excel Add-ins option is selected.

    FIGURE 26-4 Make sure to select Excel Add-Ins, not COM Add-Ins, from the drop-down menu.

  4. Click Go. Excel displays the familiar Add-Ins dialog box, shown in Figure 26-5.

  5. In the Add-Ins dialog box, click the Browse button.

  6. Browse to where you saved the file. Highlight the add-in and click OK.

images Note

Excel might prompt you to copy the add-in to its AddIns folder. I do not do this because the folder is hard to find, especially if I need to update the file.

The add-in is now installed. If you allow it, Excel copies the file from where you saved it to the default AddIns folder. In the Add-ins dialog box, the title of the add-in and comments as specified in the File Properties dialog box are displayed (see Figure 26-5).

The figure shows the Add-Ins dialog box. The add-in, TextToValues, is selected. Information about the add-in is shown at the bottom of the dialog box.

FIGURE 26-5 The add-in is now available for use.

Add-in security

Remember that anyone can go to the VB Editor, select your add-in, and change the IsAddin property to False to unhide the workbook. You can discourage this process by locking the .xlam project for viewing and protecting it in the VB Editor, but be aware that plenty of vendors sell a password-hacking utility for less than $40. To add a password to your add-in, follow these steps:

  1. Go to the VB Editor.

  2. Select Tools, VBAProject Properties.

  3. Select the Protection tab.

  4. Select the Lock Project for Viewing check box.

  5. Enter the password twice for verification.

images Caution

If you protect the code and don’t include error handling, people won’t be able to click the Debug button if an error message appears. See Chapter 24, “Handling errors,” for more information on handling errors in code so that the program ends properly and still provides customers with error information they can pass to you.

Closing add-ins

Add-ins can be closed in three ways:

  • Clear the add-in from the Add-Ins dialog box. This closes the add-in for this session and ensures that it does not open during future sessions.

  • Use the VB Editor to close the add-in. In the VB Editor’s Immediate window, type this code to close the add-in:

    Workbooks("YourAddinName.xlam").Close
  • Close Excel. All add-ins are closed when Excel is closed.

Removing add-ins

You might want to remove an add-in from the list of available add-ins in the Add-Ins dialog box. There is no effective way to do this within Excel. Follow these steps:

  1. Close all running instances of Excel.

  2. Use Windows Explorer to locate the file. The file might be located in %AppData%MicrosoftAddIns.

  3. In Windows Explorer, rename the file or move it to a different folder.

  4. Open Excel. You get a note warning you that the add-in could not be found. Click OK to dismiss this warning.

  5. Select Excel Add-Ins on the Developer tab. In the Add-Ins dialog box, clear the name of the add-in you want to remove. Excel notifies you that the file cannot be found and asks whether you want to remove it from the list. Click Yes.

Using a hidden workbook as an alternative to an add-in

One cool feature of an add-in is that the workbook is hidden. This keeps most beginners from poking around and changing formulas. However, it is possible to hide a workbook without creating an add-in.

It is easy enough to hide a workbook by selecting View, Window, Hide. The trick is to then save the workbook as Hidden. With a file that is hidden, the normal File, Save choice does not work. You can save the file from the VB Editor’s Immediate window. In the VB Editor, make sure that the workbook is selected in the Project Explorer. Then, in the Immediate window, type the following:

ThisWorkbook.Save

There is a downside to using a hidden workbook: A custom ribbon tab will not be visible if the workbook it is attached to is hidden.

Next steps

Excel add-ins are a great way to distribute VBA add-ins. But what if your users aren’t allowed to use VBA or you want to distribute your add-in to a wider audience? Microsoft has introduced a new way of sharing applications with customers: Office add-ins. These are programs that, simply put, use JavaScript, HTML, and XML to put a web page in a frame on a sheet. Chapter 27 introduces you to what is involved in creating these apps and deploying them over a network.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.21.173