以下是示例代碼模塊。我在以前版本的Access中使用的方法不起作用,因爲AllModules集合不再包含Form和報表模塊的對象。如果您有代碼模塊進行備份,則必須與以下模塊分開進行。我希望從ms-access 2013數據庫中的所有表單,報表和模塊中導出vba源代碼
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:
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
結束子