vba 使用 C#/VBScript 获取 mdb 文件 (msaccess) 中的所有源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1430870/
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-11 10:48:13  来源:igfitidea点击:

Get all source code in mdb file (msaccess) using C#/VBScript

c#ms-accessvba

提问by Fidel

I would like to get all the source code from an Accessdatabase (forms, modules) without using Office interops.

我想在不使用 Office 互操作的情况下从Access数据库(表单、模块)中获取所有源代码。

I liked the AllCodeToDesktop()function found here. However that runs from inside the mdbfile.

我喜欢这里AllCodeToDesktop()功能。但是,它是从mdb文件内部运行的。

What would a VBScript file be that loads the mdb file and runs that function on it?

加载 mdb 文件并在其上运行该函数的 VBScript 文件是什么?

采纳答案by Albert D. Kallal

To save a lot of time, is there any reason why you don't use the built in documenter?

为了节省大量时间,您是否有任何理由不使用内置文档器?

Tools->analyze->documenter

工具->分析->文档

The above has the ability to produce a report that has all code in forms, reports, modules and class modules. This report can be send to word or even saved as a text file.

上面有能力生成一个报告,其中包含表单、报告、模块和类模块中的所有代码。该报告可以发送到 word 甚至保存为文本文件。

I only mentioning this to save you time and effort. There not a need to write any code here.

我提到这个只是为了节省你的时间和精力。这里不需要写任何代码。

more notes: I don't think you can do this external. You can use ms-access as com object, and use code to pull out the code as text, but you need access installed on that computer.

更多注意事项:我认为您无法在外部执行此操作。您可以使用 ms-access 作为 com 对象,并使用 code 将代码提取为文本,但您需要在该计算机上安装 access。

There is two approaches, one is to use the saveastext command. This will export any form, report, code etc as a text file (this what the source code add-ins all use).

有两种方法,一种是使用 saveastext 命令。这会将任何表单、报告、代码等导出为文本文件(这是源代码加载项都使用的)。

The 2nd approach is to write automation code. Here is code in native access (VBA), but you just as well write this code in any language that supports com objects. The code will look like:

第二种方法是编写自动化代码。这是本机访问 (VBA) 中的代码,但您也可以使用支持 com 对象的任何语言编写此代码。代码将如下所示:

  Dim db               As Database
  Dim Doc              As Document
  Dim mdl              As Module
  Dim lngCount         As Long
  Dim strForm          As String
  Dim strOneLine       As String
  Dim sPtr             As Integer

  Set db = CurrentDb()
  ' count module LOC

  For Each Doc In db.Containers("Modules").Documents
      DoCmd.OpenModule Doc.Name

      Set mdl = Modules(Doc.Name)

      lngCount = lngCount + mdl.CountOfLines
      For i = 1 To lngCount
         strOneLine = mdl.Lines(i, 1)
         Debug.Print strOneLine
      Next i

      Set mdl = Nothing
      DoCmd.Close acModule, Doc.Name

  Next Doc

The above code can be easliy modifed to seach, or export the text. I think using saveastext is less code however, but then you get the forms definition etc, and that likely not what you want.

上面的代码可以很容易地修改为搜索,或导出文本。我认为使用 saveastext 代码较少,但是随后您会获得表单定义等,这可能不是您想要的。

回答by Robert Harvey

I would like to get all the source code from an access database (forms, modules) - without using office interops

我想从访问数据库(表单、模块)中获取所有源代码 - 不使用办公互操作

Microsoft Office is required. You might as well paste your function into the MDB and run it from there.

需要 Microsoft Office。您不妨将您的函数粘贴到 MDB 中并从那里运行它。

回答by Dale

If it is for a repository, you may want to look at Visual SourceSafe and get the Access VSS addin (the link is for Access 2003, have not tried other versions) http://www.microsoft.com/downloads/details.aspx?familyid=2ea45ff4-a916-48c5-8f84-44b91fa774bc&displaylang=en

如果是用于存储库,您可能需要查看 Visual SourceSafe 并获取 Access VSS 插件(该链接适用于 Access 2003,尚未尝试其他版本)http://www.microsoft.com/downloads/details.aspx ?familyid=2ea45ff4-a916-48c5-8f84-44b91fa774bc&displaylang=en

I am guessing that Access addin will expose a COM interface that you could extract the source reasonably cleanly into your C# app if you wanted to go that route. In either case, as Robert noted, Office is still required.

我猜测 Access 插件将公开一个 COM 接口,如果您想走那条路,您可以将源相当干净地提取到您的 C# 应用程序中。无论哪种情况,正如罗伯特所指出的,Office 仍然是必需的。

回答by David-W-Fenton

I am a lowly Access programmer and can't say that I understand your question.

我是一个低级的 Access 程序员,不能说我理解你的问题。

So far as I can tell, to get what you want, you're going to have to automate Access via COM -- there is no other way. The routines to do this are version-agnostic, and will work in all versions of Access from at least 97, as long as the version of Access you're automating is forward-compatible with the files you're examining (i.e., A2000 will be unable to examine an ACCDB and may (or may not) give unreliable results for A2002, A2003 and A2007 MDBs).

据我所知,要获得您想要的东西,您将不得不通过 COM 自动化访问——别无他法。执行此操作的例程与版本无关,并且可以在至少 97 的所有 Access 版本中使用,只要您自动化的 Access 版本与您正在检查的文件向前兼容(即,A2000 将无法检查 ACCDB,并且可能(或可能不会)给出 A2002、A2003 和 A2007 MDB 的不可靠结果)。

The key is using Application.SaveAsText to save all your code-bearing objects. This would mean you'd want to loop through all the modules, forms and reports and for the ones with code modules, output with SaveAsText. For forms and reports you may want to check the HasModule property (though this is available only in design or form view, i.e., the form has to be open) if you want to skip all objects that lack modules.

关键是使用 Application.SaveAsText 来保存所有代码承载对象。这意味着您需要遍历所有模块、表单和报告,对于带有代码模块的模块,则使用 SaveAsText 输出。对于表单和报表,如果您想跳过所有缺少模块的对象,您可能需要检查 HasModule 属性(尽管这仅在设计或表单视图中可用,即表单必须打开)。

If you don't care about A97 compatibility, it's much easier. You can use CurrentProject.AllForms, CurrentProject.AllReports and CurrentProject.AllModules. If you're not concerned with differentiating forms/reports with code modules and those without at this stage, you won't need to open them to check the HasModule property. Instead, you can review the resulting exported text file. If there is no module, the tag CodeBehindForm will be missing from the output file.

如果你不在乎A97的兼容性,那就简单多了。您可以使用 CurrentProject.AllForms、CurrentProject.AllReports 和 CurrentProject.AllModules。如果您在此阶段不关心区分带有代码模块的表单/报表和没有代码模块的表单/报表,则无需打开它们来检查 HasModule 属性。相反,您可以查看生成的导出文本文件。如果没有模块,则输出文件中将缺少 CodeBehindForm 标记。

However, it's very common for forms to have a module but no actual code. Those modules will be listed in the SaveAsText output like this:

然而,表单有一个模块但没有实际代码是很常见的。这些模块将在 SaveAsText 输出中列出,如下所示:

  CodeBehindForm
  Attribute VB_GlobalNameSpace = False
  Attribute VB_Creatable = True
  Attribute VB_PredeclaredId = True
  Attribute VB_Exposed = False
  Option Compare Database
  Option Explicit

The Option Explicit may or may not be there, and the Option Compare Database is technically not required, but 99% of modules created in Access will have one or both (only databases created in Access 2000 with default settings will lack Option Explicit). Code will start after the 4 attributes. If it's just the two OPTION statements, then there's no real code behind the form.

Option Explicit 可能存在也可能不存在,并且从技术上讲不需要 Option Compare Database,但是在 Access 中创建的 99% 的模块将具有一个或两个(只有在 Access 2000 中使用默认设置创建的数据库才会缺少 Option Explicit)。代码将在 4 个属性之后开始。如果只是两个 OPTION 语句,那么表单背后就没有真正的代码。

You should be able to do COM automation of Access from vbScript on any PC with a version of Access installed that is forwardly compatible with all the MDBs/ACCDBs you want to examine.

您应该能够在安装了与您要检查的所有 MDB/ACCDB 向前兼容的 Access 版本的任何 PC 上从 vbScript 执行 Access 的 COM 自动化。