Microsoft Office 2000/Visual Basic Programmer's Guide   

Indentation

You indent code and comments within a procedure by using a two- to four-space tab stop. (The Visual Basic Editor uses a four-space tab stop by default.) Like white space, indents are used to organize code logically and make it visually appealing. The benefits from this simple technique are substantial. To see VBA and VBScript examples that do not use the general formatting techniques discussed here, take a look at the VBA examples in the modBadCodeFormatting module in FormattingExamples.doc and the script example in ScriptBadFormatting.htm in the ODETools\V9\Samples\OPG\Samples\CH03 subfolder on the Office 2000 Developer CD-ROM. To see examples that do use the formatting techniques recommended here, see the rest of the sample code in this book.

The following list contains some general guidelines regarding where, when, and how to correctly use indentation to make your code more readable and maintainable:

Take a look at how these general techniques are applied in the following procedure:

Function GetFileList(strDirPath As String, _
                     Optional strFileSpec As String = "*.*", _
                     Optional strDelim As String = ",") As String
   
   ' This procedure returns a delimited list of files from the
   ' strDirPath directory that match the strFileSpec argument.
   ' The default delimiter character is a comma. By default, the
   ' procedure returns all files ("*.*") from the designated
   ' directory.
      
   Dim strFileList      As String ' Used to collect the file list.
   Dim strFileNames   As String ' The full path and criteria to search for.
   Dim strTemp         As String ' Temporarily holds the matching file name.
   
   ' Make sure that strDirPath ends in a "\" character.
   If Right$(strDirPath, 1) <> "\" Then
      strDirPath = strDirPath & "\"
   End If
   
   ' This will be our file search criteria.
   strFileNames = strDirPath & strFileSpec
   
   ' Create a list of matching files delimited by the
   ' strDelim character.
   strTemp = Dir$(strFileNames)
   Do While Len(strTemp) <> 0
      strFileList = strFileList & strTemp & strDelim
      strTemp = Dir$()
   Loop
   
   If Len(strFileList) > 1 Then
      ' If there are matching files, remove the delimiter
      ' character from the end of the list.
      GetFileList = Left(strFileList, Len(strFileList) - 1)
   Else
      GetFileList = ""
   End If
End Function

The GetFileList procedure is available in the modAdditionalSamples module in FormattingExamples.doc in the ODETools\V9\Samples\OPG\Samples\CH03 subfolder on the Office 2000 Developer CD-ROM.