2013-08-06 43 views
1

以下是示例代碼模塊。我在以前版本的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 

結束子

回答

2

你可以試試下面的代碼。它將導出所有模塊,類和表單代碼並保存到當前項目文件夾。記住指/檢查的Microsoft Visual Basic for Applications擴展庫VBE>工具>參考

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 
相關問題