Can I get the comment text in VBA code

Say I have the following:

Public Sub Information()
    'TEST
End Sub

Is there any way to get a "TEST" as a result? Somehow through VBA?

eg. - In PHP there is a good way to take comments. Any ideas here?

Edit: There must be a way because tools like MZ-Tools can provide comments when creating documentation.

+4
source share
4 answers

After some tests, I got to this solution:

just pass in the name of the function code module and print all the lines of comments. Inline comments will not work (you have to change the condition)

Function findComments(moduleName As String)

Dim varLines() As String
Dim tmp As Variant

With ThisWorkbook.VBProject.VBComponents(moduleName).CodeModule

    'split the lines of code into string array
    varLines = Split(.lines(1, .CountOfLines), vbCrLf)
End With

'loop through lines in code
For Each tmp In varLines

    'if line starts with '
    If Trim(tmp) Like "'*" Then

        'print comment line
        Debug.Print Trim(tmp)
    End If
Next tmp
End Function
+3
source

Microsoft Visual Basic :

'Requires reference to Microsoft Visual Basic for Applications Extensibility 
'and trusted access to VBA project object model.
Public Sub Information()
    'TEST
End Sub

Public Sub Example()
    Dim module As CodeModule
    Set module = Application.VBE.ActiveVBProject.VBComponents(Me.CodeName).CodeModule

    Dim code As String
    code = module.lines(module.ProcStartLine("Information", vbext_pk_Proc), _
                        module.ProcCountLines("Information", vbext_pk_Proc))
    Dim lines() As String
    lines = Split(code, vbCrLf)
    Dim line As Variant
    For Each line In lines
        If Left$(Trim$(line), 1) = "'" Then
            Debug.Print "Found comment: " & line
        End If
    Next
End Sub

, , (, Me CodeModule). , .

+4

, VBA ( VBIDE API). Microsoft Visual Basic Extentibility 5.3, , CodePane VBComponent:

Sub FindComments()

    Dim component As VBComponent
    For Each component In Application.VBE.ActiveVBProject.VBComponents
        Dim contents As String
        contents = component.CodeModule.Lines(1, component.CodeModule.CountOfLines)
        '"contents" now contains a string with the entire module code.

        Debug.Print ParseComments(contents) 'todo

    Next

End Sub

, contents, ... - :

Sub Test()
    Dim foo 'this is comment 1

    'this _
       is _
            comment 2

    Debug.Print "This 'is not a comment'!"

    '..and here comment 3

    REM oh and guess what, a REM instruction is also a comment!
    Debug.Print foo : REM can show up at the end of a line, given an instruction separator
End Sub

So, you need to iterate over the lines, keep track of whether the comment continues on the next line / continues from the previous line, skip string literals, etc.

Good luck

+4
source

You can try to read a line in a line of code in your module. Here is just an idea returning the first comment for further improvements:

Sub callIt()

    Debug.Print GetComment("Module1")

End Sub
Function GetComment(moduleName As String)
    Dim i As Integer
    With ThisWorkbook.VBProject.VBComponents(moduleName).CodeModule
        For i = 1 To .CountOfLines

            If Left(Trim(.Lines(i, 1)), 1) = "'" Then
                'here we have comments
                'return the first one
                GetComment = .Lines(i, 1)
                Exit Function
            End If
        Next i
    End With


End Function

Attention! in the help window, add it to Microsoft Visual Basic for application extensibility.

+1
source

Source: https://habr.com/ru/post/1660810/


All Articles