[英]Run VBA Code in Excel to obtain VBA in Access Databases
I would like to create a program in Excel that loops through a list of Access databases and writes the VBA that exists in the Access modules. 我想在Excel中创建一个程序,该程序循环访问Access数据库列表并写入Access模块中存在的VBA。 I have found some code that I can run from Access which writes the VBA that exists in the Access modules.
我找到了一些可以从Access运行的代码,该代码编写了Access模块中存在的VBA。 I am trying to figure out how to reference the database files from Excel and run the program on each database file.
我试图弄清楚如何从Excel引用数据库文件并在每个数据库文件上运行程序。 I will probably be able to figure out how to loop through the database files.
我大概能够弄清楚如何遍历数据库文件。 I just need help with referencing the database file in the below code.
我只需要在以下代码中引用数据库文件的帮助。
I can open the database with something like this: 我可以使用以下内容打开数据库:
Dim cstrDbFile As String = "C:\Database51.accdb"
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Run cstrDbFile
I also tried to set up a reference to Access like this: 我还尝试设置对Access的引用,如下所示:
Dim appAccess As Object
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase ("C:\Database51.accdb")
I need to figure out how to refer to the Access database in: 我需要找出如何在以下位置引用Access数据库:
Application.VBE.ActiveVBProject.VBComponents
I probably need to figure out how to create a reference to replace ActiveVBProject. 我可能需要弄清楚如何创建一个引用来替换ActiveVBProject。
Below is some code I found which writes the contents of VBA modules. 下面是我发现的一些代码,这些代码写了VBA模块的内容。 I don't remember where I found it.
我不记得在哪里找到它。
For Each Component In Application.VBE.ActiveVBProject.VBComponents
With Component.CodeModule
'The Declarations
For Index = 1 To .CountOfDeclarationLines
Debug.Print .Lines(Index, 1)
Next Index
'The Procedures
For Index = .CountOfDeclarationLines + 1 To .CountOfLines
Debug.Print .Lines(Index, 1)
Next Index
End With
Next Component
The following code will let you see Access database objects, but I don't know how to export the code (DoCmd not in Excel?). 以下代码将使您看到Access数据库对象,但是我不知道如何导出代码(DoCmd是否不在Excel中?)。 Your task would be VERY simple to do from Access, so I would reconsider...
在Access中,您的任务非常简单,因此我会重新考虑...
Option Explicit
' Add a reference to the DAO Object Library
Sub Read_Access_VBA()
Dim dbs As DAO.Database
Dim ctr As DAO.Container
Dim doc As DAO.Document
Dim iC As Integer
Dim iD As Integer
Dim i As Integer
Dim mdl As Module
Set dbs = DBEngine.OpenDatabase("c:\TEMP\106thRoster.mdb", False, False, _
"MS Access;")
Debug.Print "----------------------------------------"
For iC = 0 To dbs.Containers.Count - 1
Debug.Print "Container: " & dbs.Containers(iC).Name
If dbs.Containers(iC).Documents.Count > 0 Then
For iD = 0 To dbs.Containers(iC).Documents.Count - 1
Debug.Print vbTab & "Doc: " & dbs.Containers(iC).Documents(iD).Name
Next iD
Else
Debug.Print " No Documents..."
End If
Next iC
'Set ctr = dbs.Containers!Modules
dbs.Close
Set doc = Nothing
Set ctr = Nothing
Set dbs = Nothing
End Sub
I was able to find some code that will assist me with my final goal: Exporting MS Access Forms and Class / Modules Recursively to text files? 我能够找到一些有助于最终目标的代码:将MS Access表单和类/模块递归导出到文本文件?
Below are the most significant lines that will allow me to make progress with the project. 以下是最重要的几行,它们将使我在项目上取得进展。
LineCount = oApp.Forms(Name).Module.CountOfLines
FileName = Path & "\" & Name & ".vba"
F = FreeFile
Open FileName For Output Access Write As #F
Print #F, oApp.Forms(Name).Module.Lines(1, LineCount)
Close #F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.