如何通过c#从超过一百个Access数据库中提取所有VBA代码(表单,模块,报告)。
无法更改数据库和/或向其添加vba代码。代码的提取必须以只读方式完成。
我试过这段代码:
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();
但是这段代码存在一些问题:
编辑:
我找到了阅读所有代码的方法:
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");
}
这很好用。但主要问题保持不变:
执行autoexec代码,如果内部有错误,则提取停止。