Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleExtract comments from a Visual Basic 6 file in Visual Basic 6
DescriptionThis example shows how to extract comments from a Visual Basic 6 file in Visual Basic 6
Keywordsextract comments, comments, Visual Basic 6, Attribute, code
CategoriesStrings, Tips and Tricks, Files and Directories, Software Engineering
 
The idea behind this example is to extract comments from a Visual Basic code file so you can spellcheck them. This is a fairly simple, manual approach. Some commercial products can spellcheck comments and strings for you.

Function ExtractComments reads a Visual Basic file stored in a string and returns a string containing its comments.

The function starts by removing line continuation characters (" _") followed by a vbCrLf. This joins continued lines.

Next the code splits the file into lines. It calls function FirstCodeLine to find the first line in the file that is actual code. The program then loops through the lines containing code.

For each line, the program loops through the line's characters. When it finds a double quote character, the code toggles the Boolean variable quote_open to remember whether it is inside a quoted string.

When it finds a single quote character, the code checks quote_open to see if the character is inside a quoted string. If it is not, then the function adds the rest of the line to the result string and exits the inner For loop so it goes to check the next line.

After it has checked every line, the function returns the result it has built.

 
' Extract the comments from this text.
Private Function ExtractComments(ByVal txt As String) As _
    String
Dim lines() As String
Dim first_line As Long
Dim line_num As Long
Dim i As Long
Dim ch As String
Dim quote_open As Boolean
Dim comment As String
Dim result As String

    ' Remove line continuation characters.
    txt = Replace(txt, " _" & vbCrLf, " ")

    ' Split the text into lines.
    lines = Split(txt, vbCrLf)

    ' Find the first code line.
    first_line = FirstCodeLine(lines)

    ' Process each line.
    result = ""
    For line_num = first_line To UBound(lines)
        comment = ""
        quote_open = False
        For i = 1 To Len(lines(line_num))
            ch = Mid$(lines(line_num), i, 1)
            If ch = """" Then
                ' Start or end a quoted string.
                quote_open = Not quote_open
            ElseIf ch = "'" Then
                ' If it's not in a quoted string
                ' then it starts a comment.
                If Not quote_open Then
                    comment = Mid$(lines(line_num), i)
                    Exit For
                End If
            End If
        Next i
        If Len(comment) > 0 Then result = result & comment _
            & vbCrLf
    Next line_num

    ExtractComments = result
End Function
 
Visual Basic 6 files begin with some header information. For example, .frm files begin with a description of the form's menus. The header ends with one or more "Attribute" statements. Function FirstCodeLine loops through the lines in a file looking for those Attribute statements. It returns the index of the first line of code after the Attribute statements.
 
' Skip the file's header information up to the first line
'after the initial Attributes statements.
Private Function FirstCodeLine(lines() As String) As Long
Dim i As Integer
Dim found_attributes As Boolean

    For i = LBound(lines) To UBound(lines)
        If StartsWith(lines(i), "Attribute ") Then
            ' This is an attribute line.
            found_attributes = True
        Else
            ' See if we already passed the attributes
            If found_attributes Then
                FirstCodeLine = i
                Exit Function
            End If
        End If
    Next i

    ' This shouldn't happen.
    ' Start with the first line.
    FirstCodeLine = LBound(lines)
End Function
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated