Reputation: 75
In Visual Basic for Applications (VBA), you can set the attributes of a module or a variable using the Attribute keyword. For instance
' Header
Attribute VB_Name = "ClassOrModuleName"
Attribute VB_GlobalNameSpace = False ' ignored
Attribute VB_Creatable = False ' ignored
Attribute VB_PredeclaredId = False ' a Value of True creates a default global instance
Attribute VB_Exposed = True ' Controls how the class can be instanced.
'Module Scoped Variables
Attribute variableName.VB_VarUserMemId = 0 ' Zero indicates that this is the default member of the class.
Attribute variableName.VB_VarDescription = "some string" ' Adds the text to the Object Browser information for this variable.
'Procedures
Attribute procName.VB_Description = "some string" ' Adds the text to the Object Browser information for the procedure.
Attribute procName.VB_UserMemId = someInteger
' 0: Makes the function the default member of the class.
' -4: Specifies that the function returns an Enumerator.
More information about them in: https://christopherjmcclellan.wordpress.com/2015/04/21/vb-attributes-what-are-they-and-why-should-we-use-them/
I was thinking, is there a way to get/read these attributes inside the code? For instance something like
Sub BarMacroName()
'
' BarMacroName Macro
' Bar Macro Description
'
Dim var
MsgBox VB_Description 'display this module's description
MsgBox VB_Name 'display this module's description
End Sub
Not just the description and the name, but in general, can we actually read the attributes inside the code itself?
Edit: I'm specifically looking to see if you can extract the Attribute value within the VBA script itself. I'm researching malware vulnerabilities and I was curious if someone could embed malicious code within, let's say, the description of a VBA module.
Upvotes: 5
Views: 999
Reputation: 3205
In a word, yes there is a way to read annotations inside your VBA code. You can view and modify attributes using Rubberduck, a free add-in to the MS Offce Visual Basic Editor.
Rubberduck shows existing attributes by adding annotations (basically a specially formatted comment) to your code. Add attributes by adding annotations and clicking a sync button. Remove attributes by deleted the commented annotation and clicking a sync button.
Here's an example of annotations in a code module.
'@Exposed
'@Folder("Class Libraries.Main.Book Sheet Range Project")
'@ModuleDescription("Class with functions and methods for working with Excel Workbooks.")
'@PredeclaredID
Option Explicit
Rubberduck warns the user when an annotation and attribute are out of sync and allows you to sync it to whichever one is correct.
The add-in does much more than managing annotations and is definitely worth checking out. If nothing else, it beats the alternative: exporting your modules, modifying attributes in Notepad and re-importing them to your project.
Upvotes: 1