1

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.

Gener4tor
  • 414
  • 3
  • 12
  • 40
  • For the first problem why not just copy the db, run your code on it then delete it. For the second dump the module names and look at the ones that are excluded to see if they have anything in common. – Alex K. Jun 12 '18 at 12:07
  • Copying the database does not prevent the autoexec-macro from running. I dont see how copying the db should help me. – Gener4tor Jun 12 '18 at 12:20
  • because if its a self contained mdb who cares what a temporary copy does to itself. – Alex K. Jun 12 '18 at 12:24
  • the vba Code which is executed in autoexec can doa lot of things. e.g. creating word documents, deleting files, writing data to a remote mySQL-Server, connecting to SAP and so on...so this is no option for me. – Gener4tor Jun 12 '18 at 12:32
  • And I cant see any difference between the modules I can read and the one i cant. As there are literally no properties for a module other than the Name... – Gener4tor Jun 12 '18 at 13:44

0 Answers0