简体   繁体   English

我希望从 ms-access 2013 数据库中的所有表单、报告和模块中导出 vba 源代码

[英]I wish to export the vba source code from all of the Forms, Reports, and Modules in an ms-access 2013 database

Here is a sample code module.这是一个示例代码模块。 The methods I used in previous version of Access do not work because the AllModules collection no longer contains objects for Form and report modules.我在以前版本的 Access 中使用的方法不起作用,因为 AllModules 集合不再包含用于窗体和报表模块的对象。 If you have a code module to back up, it must be done separately with the following module.如果您有要备份的代码模块,则必须与以下模块分开完成。

Public Sub ExportModules2013()

Dim boolCloseModule As Boolean
Dim frm As Form
Dim i As Integer
Dim iModuleType As Integer
Dim modModule As Module
Dim modOpenModules As Modules
Dim obj As AccessObject, dbs As Object
Dim rpt As Report
Dim sFileName As String
Dim sPath As String
'
sPath = "X:\Perryaire\Source\201308025\"
'
Set dbs = Application.CurrentProject
' Search for all AccessObject objects in AllForms Collection.
For Each obj In dbs.AllForms
    DoCmd.OpenForm obj.Name, acDesign
    Set frm = Forms(obj.Name)
    If frm.HasModule Then
        Set modModule = frm.Module
        GoSub L_ExportModule
    End If
DoCmd.Close acForm, frm.Name
Set frm = Nothing
Next obj
' Search for all AccessObject objects in AllReports Collection.
For Each obj In dbs.AllReports
    DoCmd.OpenReport obj.Name, acDesign
    Set rpt = Reports(obj.Name)
    If rpt.HasModule Then
        Set modModule = rpt.Module
        GoSub L_ExportModule
    End If
DoCmd.Close acReport, rpt.Name
Set rpt = Nothing
Next obj
' Search for all AccessObject objects in AllModules collection.
For Each obj In dbs.AllModules
    If Not obj.IsLoaded Then
        DoCmd.OpenModule (obj.Name)
    End If
    Set modModule = Application.Modules(obj.Name)
    GoSub L_ExportModule
    If boolCloseModule Then DoCmd.Close acModule, modModule.Name
Next obj
Exit Sub

L_ExportModule: L_ExportModule:

With modModule
    iModuleType = acStandardModule
    On Error Resume Next
    iModuleType = .Type
    sFileName = Nz(.Name, "MISSING:") & IIf(iModuleType = acStandardModule, ".bas", ".cls")

Lopen:洛本:

    On Error GoTo Lmkdir
    Open sPath & sFileName For Output As #1
    If modModule.Type = acStandardModule Then
        Print #1, "Attribute VB_Name = """ & .Name & """"
    Else
        Print #1, "VERSION 1.0 CLASS"
        Print #1, "BEGIN"
        Print #1, "   MultiUse = -1  'True"
        Print #1, "End"
        Print #1, "Attribute VB_Name = """ & .Name & """"
        Print #1, "Attribute VB_GlobalNameSpace = False"
        Print #1, "Attribute VB_Creatable = False"
        Print #1, "Attribute VB_PredeclaredId = False"
        Print #1, "Attribute VB_Exposed = False"
    End If
    Print #1, .Lines(1, CLng(.CountOfLines))

    Close #1
End With
Return

Lmkdir:目录:

If Err.Number = "76" Then
    MkDir sPath
    Resume Lopen
Else
    MsgBox "Error: " & Err.Number & " " & Err.Description
End If

Exit Sub

End Sub结束子

You can try the following code.你可以试试下面的代码。 It will export all the module, class and form code and save to the current project folder.它将导出所有模块、类和表单代码并保存到当前项目文件夹。 Remember to refer to / Check the Microsoft Visual Basic For Applications Extensibility Library in VBE > Tools > References记得在VBE > Tools > References 中参考/检查Microsoft Visual Basic For Applications Extensibility Library

Sub ExportAllCode()

  For Each c In Application.VBE.VBProjects(1).VBComponents
      Select Case c.Type
          Case vbext_ct_ClassModule, vbext_ct_Document
              Sfx = ".cls"
          Case vbext_ct_MSForm
              Sfx = ".frm"
          Case vbext_ct_StdModule
              Sfx = ".bas"    
          Case Else
              Sfx = ""
      End Select
      If Sfx <> "" Then
          c.Export _
              fileName:=CurrentProject.Path & "\" & _
              c.Name & Sfx
      End If
  Next c

  End Sub

Have you tried to use the undocumented Application.SaveAsText functions?您是否尝试过使用未记录的Application.SaveAsText函数?

Here is what I use to export all forms/reports/queries/modules:这是我用来导出所有表单/报告/查询/模块的内容:

Another related question:另一个相关问题:

There is tool like this for Excel. Excel 有这样的工具。 Maybe you can look at the source and use the parts you want.也许你可以看一下源码,用你想要的部分。 http://www.codeproject.com/Articles/18029/SourceTools-xla http://www.codeproject.com/Articles/18029/SourceTools-xla

屏幕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM