首页 > 解决方案 > 如何将 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 中

标签: pythonexcelvbawin32com

解决方案


也许这接近您正在寻找的东西?

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它可以工作,仅供参考。


推荐阅读