4.2 Modules

A module is the fundamental syntactic unit of VBA source code. The physical representation of a module is implementation dependent but logically a VBA module is a sequence of Unicode characters that conform to the VBA language grammars.

A module consists of two parts: a module header and a module body.

The module header is a set of attributes consisting of name/value pairs that specify the certain linguistic characteristics of a module. While a module header could be directly written by a human programmer, more typically a VBA implementation will mechanically generate module headers based upon the programmer’s usage of implementation specific tools. These tools are not part of the scope of this document, so their contents, including the version and all text between "BEGIN" and "END" at the start of the file is not part of the module body and is not required to conform to the VBA grammar.

A module body consists of actual VBA Language source code and most typically is directly written by a human programmer.

VBA supports two kinds of modules, procedural modules and class modules, whose contents MUST conform to the grammar productions <procedural-module> and <class-module>, respectively:

 procedural-module = LINE-START procedural-module-header EOS 
                      LINE-START procedural-module-body 
  class-module = LINE-START class-module-header  
                 LINE-START class-module-body 
   
  procedural-module-header = attribute "VB_Name" attr-eq quoted-identifier attr-end 
   
  class-module-header = 1*class-attr 
   
  class-attr = attribute "VB_Name" attr-eq quoted-identifier attr-end 
       /  attribute "VB_GlobalNameSpace" attr-eq "False" attr-end 
       /  attribute "VB_Creatable" attr-eq "False" attr-end 
       /  attribute "VB_PredeclaredId" attr-eq boolean-literal-identifier attr-end 
       /  attribute "VB_Exposed" attr-eq boolean-literal-identifier attr-end 
       /  attribute "VB_Customizable" attr-eq boolean-literal-identifier attr-end 
  attribute = LINE-START "Attribute" 
  attr-eq = "=" 
  attr-end = LINE-END 
   
  quoted-identifier = double-quote NO-WS IDENTIFIER NO-WS double-quote  

Static Semantics.

  • The name value (section 3.3.5.1) of an <IDENTIFIER> that follows an <attribute> element is an attribute name.

  • An element that follows an <attr-eq> element defines the attribute value for the attribute name that precedes the same <attr-eq>.

  • The attribute value defined by a <quoted-identifier> is the name value of the contained identifier.

  • The last <class-attr> for a specific attribute name within a given <class-module-header> provides the attribute value for its attribute name.

  • If an <class-attr> for a specific attribute name does not exist in an <class-module-header> it is assumed that a default attribute value is associated with the attribute name according to the following table:

     

    Attribute Name

    Default Value

    VB_Creatable

    False

    VB_Customizable

    False

    VB_Exposed

    False

    VB_GlobalNameSpace

    False

    VB_PredeclaredId

    False

     

  • The module name of a module is the attribute value of the module’s VB_NAME attribute.

  • A maximum length of a module name is 31 characters.

  • A module name SHOULD NOT be a <reserved-identifier>.

  • A module’s module name might not be the same as the project name (section 4.1) of the project that contains the module or that of any project (section 4.1) referenced by the containing project.

  • Every module contained in a project MUST have a distinct module name.

  • Both the VB_GlobalNamespace and VB_Creatable attributes MUST have the attribute value "False" in a VBA module that is part of a VBA source project (section 4.1). However library projects (section 4.1) can contain modules in which the attributes values of these attributes are "True".

  • In addition to this section, the meaning of certain attributes and attribute combinations when used in the definition of class modules is defined in section 5.2.4.1. All other usage and meanings of attributes are implementation-dependent.