gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:50:31 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com