Freigeben über


Building Reusable Code Libraries

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Once you've been writing VBA code for any length of time, you'll find that you need to perform certain operations regularly. For example, you may often need to parse a file path and return just the name of the file. Or you may need to write procedures to log errors to a text file. Rather than re-creating these procedures each time you need them, you can store them in a code library and reuse them in other VBA projects.

There are two ways that you can create a reusable code library:

  • By creating a DLL in Visual Basic 4.0 or later

  • By creating an application-specific template or add-in

Whatever method you choose, you must always set a reference from the VBA project to the file containing the code library. Even if the file is a template or add-in, the procedures it contains will not be available to your VBA project unless you set a reference to it. Once you do set a reference to a code library, you can call its procedures from your VBA project.

The ODETools\V9\Samples\OPG\Samples\CH11\CodeLib subfolder on the Office 2000 Developer CD-ROM contains examples of both types of code libraries. These sample code libraries contain procedures from Chapter 7, "Getting the Most Out of Visual Basic for Applications," plus some additional procedures that you may find useful. The files that contain code libraries are CodeLib.dll, which can be used from any VBA host application, and CodeLib.dot, which can be used only from Word.