Lets say I have the following:
Public Sub Information()
'TEST
End Sub
Is there a way to get "TEST" as a result? Somehow through VBA?
E.g. - In PHP there is a good way to take the comments. Any ideas here?
Edit: There should be a way, because tools like MZ-Tools are able to provide the comments when they generate the documentation.
After some tests, I got to this solution:
simply pass the name of the code-module to the function and it will print all comment lines. Inline comments won't work(you have to change the condition)
You could try with reading line by line of code in your module. Here is just idea returning first comment for further improvements:
Important! in Reference window add one to 'Microsoft Visual Basic for Applications Extensibility'.
You can use Microsoft Visual Basic for Applications Extensibility to examine code at runtime:
Note that the above example assumes that it's running in a Worksheet or Workbook code module (hence
Me
when locating theCodeModule
). The best method for locating the correct module will depend on where you want to locate the procedure.You need to parse the code yourself, using the VBA Extensibility library (aka "VBIDE API"). Add a reference to the Microsoft Visual Basic for Applications Extentibility 5.3 type library, and then you can access types such as
CodePane
andVBComponent
:Once you have a module's
contents
, you need to implement logic to find comments... and that can be tricky - here's some sample code to play with:So you need to iterate the lines, track whether the comment is continuing on the next line / continued from the previous line, skip string literals, etc.
Have fun!