This documentation is archived and is not being maintained.

Using Constants

Office 2007

Your code might contain frequently occurring constant values, or might depend on certain numbers that are difficult to remember and have no obvious meaning. You can make your code easier to read and maintain using constants. A constant is a meaningful name that takes the place of a number or string that does not change. You can't modify a constant or assign a new value to it as you can a variable.

There are three types of constants:

Intrinsic constants or system-defined constants are provided by applications and controls. Other applications that provide object libraries, such as Microsoft Access, Microsoft Excel, Microsoft Project, and Microsoft Word also provide a list of constants you can use with their objects, methods, and properties. You can get a list of the constants provided for individual object libraries in the Object Browser.

Visual Basic constants are listed in the Visual Basic for Applications type library, and Data Access Object (DAO) library.

Ee440527.vs_note(en-us,office.12).gif  Note
Visual Basic continues to recognize constants in applications created in earlier versions of Visual Basic or Visual Basic for Applications. You can upgrade your constants to those listed in the Object Browser. Constants listed in the Object Browser don't have to be declared in your application.

In earlier versions of Visual Basic, constant names were usually capitalized with underscores. For example:


Intrinsic constants are now qualified to avoid the confusion when constants with the same name exist in more than one object library, which may have different values assigned to them. There are two ways to qualify constant names:

  • By prefix
  • By library reference

Qualifying Constants by Prefix

The intrinsic constants supplied by all objects appear in a mixed-case format, with a 2-character prefix indicating the object library that defines the constant. Constants from the Visual Basic for Applications object library are prefaced with "vb" and constants from the Microsoft Excel object library are prefaced with "xl". The following examples illustrate how prefixes for custom controls vary, depending on the type library.

  • vbTileHorizontal
  • xlDialogBorder

Qualifying Constants by Library Reference

You can also qualify the reference to a constant by using the following syntax:

[libname.] [modulename.]constname

The syntax for qualifying constants has these parts:

libnameOptional. The name of the type library that defines the constant. For most custom controls (not available on the Macintosh), this is also the class name of the control. If you don't remember the class name of the control, position the mouse pointer over the control in the toolbox. The class name is displayed in the ToolTip.
modulenameOptional. The name of the module within the type library that defines the constant. You can find the name of the module by using the Object Browser.
constnameThe name defined for the constant in the type library.

For example: