从 ms access 应用程序导出整个 vba 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24780363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:52:38  来源:igfitidea点击:

Exporting the whole vba code from an ms access application

filevbams-accessexportdirectory

提问by Lamloumi Afif

I have an ms access Application ( ms access 2013) and I'd like to export all vba code to separate files in a folder

我有一个 ms access 应用程序( ms access 2013 ),我想将所有 vba 代码导出到文件夹中的单独文件

What code can i add to do this?

我可以添加什么代码来做到这一点?

Thanks,

谢谢,

回答by djikay

Try this:

尝试这个:

Public Sub ExportVBAComponents()

  Dim wbPath As String
  Dim vbComp As Object
  Dim exportPath As String

  wbPath = ActiveWorkbook.Path

  For Each vbComp In ActiveWorkbook.VBProject.VBComponents
    exportPath = wbPath & "\" & vbComp.Name & Format$(Now, "_yyyymmdd_hhnnss")

    Select Case vbComp.Type
        Case 1 ' Standard Module
            exportPath = exportPath & ".bas"
        Case 2 ' UserForm
            exportPath = exportPath & ".frm"
        Case 3 ' Class Module
            exportPath = exportPath & ".cls"
        Case Else ' Anything else
            exportPath = exportPath & ".bas"
    End Select

    On Error Resume Next
    vbComp.Export exportPath
    On Error GoTo 0
  Next

End Sub

The code above will export all the VBA components/modules in your ActiveWorkbookto the same location as your workbook. It will use the component name as part of the file name and will add a timestamp. Since you have more than 100 modules, you'd better change the export path to include a subfolder to group them all together in one place.

上面的代码会将您中的所有 VBA 组件/模块导出ActiveWorkbook到与工作簿相同的位置。它将使用组件名称作为文件名的一部分并添加时间戳。由于您有 100 多个模块,因此最好更改导出路径以包含一个子文件夹,以便将它们组合到一个位置。

NOTE:For this to work, you need to select Options>Trust Center>Trust Center Settings...>Macro Settings>Trust access to the VBA project object model. Otherwise you get some random error in the For Eachline. You can tick that option off afterwards if you're worried about it.

注意:为此,您需要选择Options>Trust Center>Trust Center Settings...>Macro Settings>Trust access to the VBA project object model。否则,您会For Each在行中遇到一些随机错误。如果您对此感到担心,可以在事后勾选该选项。