Extract VBA-Code from Access via C#

岁酱吖の 提交于 2019-12-31 05:10:11

问题


How do I extract all VBA Code (Forms, Moduls, Reports) out of more than hundred Access databases via c#.

It is not possible to change the databases an/or add vba code to them. The extraction of the Code has to be done read-only.

I tried this Code:

var appClass = new ApplicationClass();

appClass.OpenCurrentDatabase(@"C:\Temp\Test\FBIOE.mdb", false, "");

Console.WriteLine(appClass.Version);
Console.WriteLine(appClass.Modules.Count.ToString());
Console.WriteLine(appClass.Modules.Parent.ToString());

int NumOfLines = 0;
Console.WriteLine("Anzahl Module:" + appClass.Modules.Count.ToString());
for (int i = 0; i < appClass.Modules.Count; i++)
{
    Console.WriteLine(appClass.Modules[i].Name + " : " + appClass.Modules[i].CountOfLines);
    NumOfLines += appClass.Modules[i].CountOfLines;
    //Console.WriteLine(appClass.Modules[i].Name + " : " + appClass.Modules[i].ToString());
}

Console.WriteLine("Number of Lines : " + NumOfLines);
Console.ReadKey();

But this Code has some Problems:

  • It executes the autoexec-macro which is a very bad thing as all of the databases are doing different dangerouse things when starting.
  • it seems not to get every module. A lot of them seems to be skippted. In my test db there are more than 53 modules (not counting forms and reports) but appClass.Modules.Count is 44 (and there are forms and Reports included)

Edit:

I found a way to read all the Code:

    appClass.OpenCurrentDatabase(tempFile, false, "");

Debug.WriteLine("appClass.CurrentProject.AllForms.Count:" + appClass.CurrentProject.AllForms.Count.ToString());
Debug.WriteLine("appClass.CurrentProject.AllMacros.Count:" + appClass.CurrentProject.AllMacros.Count.ToString());
Debug.WriteLine("appClass.CurrentProject.AllModules.Count:" + appClass.CurrentProject.AllModules.Count.ToString());
Debug.WriteLine("appClass.CurrentProject.AllReports.Count:" + appClass.CurrentProject.AllReports.Count.ToString());
var currentProject = appClass.CurrentProject;

for (int i = 0; i < appClass.CurrentProject.AllForms.Count; i++)
{
    var form = appClass.CurrentProject.AllForms[i];
    Debug.WriteLine("Erledige: " + file+ " Item: " + form.FullName);
    appClass.SaveAsText(AcObjectType.acForm, form.FullName, absolutesVerzeichnis + "Form_"+form.FullName + ".txt");
}

for (int i = 0; i < appClass.CurrentProject.AllMacros.Count; i++)
{
    var macro = appClass.CurrentProject.AllMacros[i];
    Debug.WriteLine("Erledige: " + file + " Item: " + macro.FullName);
    appClass.SaveAsText(AcObjectType.acMacro, macro.FullName, absolutesVerzeichnis + "Makro_" + macro.FullName + ".txt");
}

for (int i = 0; i < appClass.CurrentProject.AllModules.Count; i++)
{
    var module = appClass.CurrentProject.AllModules[i];
    Debug.WriteLine("Erledige: " + file + " Item: " + module.FullName);
    appClass.SaveAsText(AcObjectType.acModule, module.FullName, absolutesVerzeichnis + module.FullName + ".txt");
}

for (int i = 0; i < appClass.CurrentProject.AllReports.Count; i++)
{
    var report = appClass.CurrentProject.AllReports[i];
    Debug.WriteLine("Erledige: " + file + " Item: " + report.FullName);
    appClass.SaveAsText(AcObjectType.acReport, report.FullName, absolutesVerzeichnis + "Report_" + report.FullName + ".txt");
}

This works fine. But the main problem stays the same:

The autoexec code is executed and if there are errors inside, the extraction stops.

来源:https://stackoverflow.com/questions/50816715/extract-vba-code-from-access-via-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!