excel - VBA:有没有办法获取/读取模块属性?

标签 excel vba ms-word

在 Visual Basic for Applications (VBA) 中,您可以使用 Attribute 关键字设置模块或变量的属性。例如

    ' 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.

有关它们的更多信息,请访问:https://christopherjmcclellan.wordpress.com/2015/04/21/vb-attributes-what-are-they-and-why-should-we-use-them/

我在想,有没有办法在代码中获取/读取这些属性?例如像
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

不仅是描述和名称,而且总的来说,我们真的可以读取代码本身内部的属性吗?

编辑:我特别想看看您是否可以在 VBA 脚本本身中提取属性值。我正在研究恶意软件漏洞,我很好奇是否有人可以在 VBA 模块的描述中嵌入恶意代码。

最佳答案

总之,是的,有一种方法可以读取 VBA 代码中的注释。您可以使用 Rubberduck(MS Office Visual Basic 编辑器的免费插件)查看和修改属性。
Rubberduck 通过向您的代码添加注释(基本上是特殊格式的注释)来显示现有属性。通过添加注释并单击同步按钮来添加属性。通过删除注释注释并单击同步按钮来删除属性。
这是代码模块中的注释示例。

'@Exposed
'@Folder("Class Libraries.Main.Book Sheet Range Project")
'@ModuleDescription("Class with functions and methods for working with Excel Workbooks.")
'@PredeclaredID
Option Explicit
当注释和属性不同步时,Rubberduck 会警告用户,并允许您将其同步到正确的那个。
该加载项不仅仅是管理注释,而且绝对值得一试。如果不出意外,它会胜过其他选择:导出模块,在记事本中修改属性并将它们重新导入项目。
Rubberduck VBA: Annotations

关于excel - VBA:有没有办法获取/读取模块属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733224/

相关文章:

cmd - 从 cmd 打印 MS Word 文件

VBA - 更短的 If/Or 语句

excel - 将给定单元格中的超链接换行

excel - 调用一个单元格,其行是另一个单元格的内容

excel - 如何使单元格范围在 VBA 中工作?

Excel VBA图表,仅在最后一点显示数据标签

vba - 使用 VBA 查找 Office 2010 Word 文档的大纲级别

c# - 在不依赖 Azure 云服务的情况下读写 Excel 的最佳方式?

javascript - 从 Morningstar.com 获取上行/下行捕获率

vba - 如何在VBA代码中通过名称引用Word字段?