python - 如何将 VBA 代码从 de 模块导出为文本?
问题描述
我正在尝试映射我在办公室的一些 excel 中的所有 VBA 代码。
在我的工作中,我们有 200 多个 excel 文件,每个文件中都有很多宏。如何提取某些模块的代码文本?
我见过类似的东西
workbook = excel.Workbooks.Open("{}{}.xlsm".format(path, file), True, True)
for i in workbook.VBProject.VBComponents:
print(i.Name)
返回
Plan1
Plan2
Main
Plan5
Plan3
Plan4
Sheet1
如何在这些模块中获取 VBA 代码?
解决方案可以在 VBA 或 Python 中
解决方案
也许这接近您正在寻找的东西?
Sub GEN_USE_Export_all_modules_from_Project()
' https://www.ozgrid.com/forum/forum/help-forums/excel-general/60787-export-all-modules-in-current-project
' reference to extensibility library
Dim tday As String
tday = Date
Dim objMyProj As VBProject
Dim objVBComp As VBComponent
Dim destFolder as String
destFolder = "C:\Users\WHATEVER\Documents\Business\EXCEL NOTES\"
Set objMyProj = Application.VBE.ActiveVBProject
tday = WorksheetFunction.Substitute(tday, "/", "-")
For Each objVBComp In objMyProj.VBComponents
If objVBComp.Type = vbext_ct_StdModule Then
objVBComp.Export destFolder & tday & " - " & objVBComp.name & ".bas"
End If
Next
MsgBox ("Saved to " & destFolder)
End Sub
这将循环通过您的 VBAProject(存储此宏本身的位置),并将模块保存在.bas
您可以打开和浏览的文件中。
编辑:您可以替换.bas
为.txt
它可以工作,仅供参考。
推荐阅读
- node-notifier - Windows 中的粘滞通知不起作用
- mysql - 将 mssql 脚本转换为表中的 mysql 更新列以及其他表中的最新日期
- sql-server - 使用 FOR XML 将行转换为逗号分隔列表时出错
- python - 如何使用 python 在 Whatsapp 中发送自动消息?
- javascript - 如何从“window.getSelection”获取字符串以及如何使用“window.open”修复打开新标签?
- amazon-web-services - 公开内部 IP,以便可以从 Internet 访问
- javascript - 跟随 JSON 进行迭代时出错
- python - 烧瓶上传CSV文件而不保存
- h2o - 如何在 H2O 服务器中禁用 TRACE 方法
- java - 布尔向量的 BitSet 表示