Code Commenter and Error Handler Template Formats

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.

The VBA Code Commenter and Error Handler Add-In makes it possible for you to apply a template to selected procedures in the code. You can choose to apply a given template to only one procedure, to all the procedures in a module, or to all the procedures in a project. By providing this functionality, this add-in saves the user time from copying and pasting the same code comments or error handling code over and over. The Code Commenter and Error Handler Add-In uses template files (.eht) to control the format of comments. These templates contain replaceable tokens that specify what will be added to your code. As the add-in is inserting text, it searches for these tokens.

Three template files are provided for use with the Code Commenter and Error Handler Add-In: CodeCommenter.eht, ErrorHandler.eht, and CC_EH.eht. The CodeCommenter template file provides a standard format for comments to be inserted into procedures. The ErrorHandler template file provides errorhandling code that would otherwise be tedious to repeatedly insert into every procedure. The CC_EH.eht file combines the two previous templates, so users can take advantage of both purposes of this add-in.

In addition, you can create custom templates using the Template Editor provided through the add-in, or by using Microsoft® Notepad or another text editor. Then, the Template to Apply can be set to the custom-made template.

To use the Template Editor provided by the add-in, click the New button on the Code Commenter and Error Handler Add-In dialog. This opens the Template Editor to a new file. Or, if you simply want to modify an existing template, set the Template to apply option to the template file that you would like to modify, and click the Edit button. Then you can change an existing template as you like, and save it either under its original name, or if you want to retain a copy of the original template file, save the modified template under a different name.

The codecommenter.eht and errorhandler.eht templates may be applied to the same procedure. The add-in only allows one error handling type template and one code commenting type template to be applied to each procedure. If a certain type of template has already been applied to a procedure, applying another template of the same type using the add-in (either the same template or a template of the same type) has no effect.

These templates contain replaceable tokens that specify what will be added to your code. As the add-in inserts text, it searches for these tokens:

Token Meaning
$$Author Author. Replaced with the current author name.
$$Initials Author initials.
$$CurrentDate Current date, formatted as Windows short date.
$$CurrentTime Current time, formatted as Windows short time.
$$HeaderComments Header comments.
$$HeaderVariables Header variables.
$$ParameterList Parameter list. Provides list of arguments to the current procedure.
$$ProcedureBody Procedure body.
$$ProcedureName Name of procedure. Replaced with the fully qualified procedure name, including the class name if it is a member of a class.
$$ProcedureAndModuleName Name of the procedure and module in the form ModuleName.ProcedureName. Note that this token is not available from the Insert menu of the editor, but is a valid, useful token.
$$ProcedureType Type of procedure. Replaced with "Sub", "Function" or "Property" as appropriate.
$$ProjectName Project name.
$$StartAuto Start auto. Used to flag the start of an inserted error handler.
$$EndAuto End auto. Used to flag the end of an inserted error handler.
$$StartHeader Start of header for Code Commenter and Error Handler.
$$EndHeader End of header for Code Commenter and Error Handler.

The $$HeaderComments, $$HeaderVariables, and $$ProcedureBody tokens are used to locate the replacement text within the existing procedure. If these are used in the template, they must appear on separate lines, in the order $$HeaderComments, $$HeaderVariables, $$ProcedureBody.

  • $$HeaderComments matches any number of comment lines or blank lines directly following the Sub, Function, or Property definition.
  • $$HeaderVariables matches any number of Dim or Static lines directly following header comments, if any.
  • $$ProcedureBody matches the remaining body of the procedure.

The $$Author and $$Initials tokens are used to insert the name and initials of the current developer. The add-in first tries to read these from the registry under HKCU/Software/Microsoft/Office Developer/10/VBA_Procedure_Templates. You enter the name and initials information in the add-in window. If new values are entered in the Name and Initials fields, the add-in uses those values and stores them in the registry for future use.

When the add-in encounters the $$StartHeader token, it inserts the comment:

' Code Header inserted by VBA Code Commenter and Error Handler Add-in

When the add-in encounters the $$EndHeader token, it inserts the comment:

' End Code Header block

If the add-in finds its $$StartHeader token in an existing procedure, it makes no modifications to that procedure.

When the add-in encounters the $$StartAuto token, it inserts the comment:

'  Error handling block added by VBA Code Commenter and Error Handler Add-in. DO NOT _
      EDIT this block of code.

When the add-in encounters the $$EndAuto token, it inserts the comment:

' End Error handling block.

If a template has already been applied to a procedure, it cannot be re-applied or replaced with another template unless the existing template is manually deleted from the procedure.

Note   To be recognized as a valid template file, the file must contain at least the following tokens: $$ProcedureBody plus either $$StartHeader and $$EndHeader or $$StartAuto and $$EndAuto. Additionally, these tokens must start on their own lines. If the required tokens are missing or are not on their own lines, an error will occur when attempting to apply the .eht file.

See Also

Code Commenter and Error Handler Template Formats | Main Dialog Box (Code Commenter and Error Handler) | Select Code Commenter or Error Handler Templates Window (Code Commenter and Error Handler) | Template Editor (Code Commenter and Error Handler)