I am working with a directory of Excel files to get information about each file. I am trying to use C# Excel interop to gather information about VBA Macros associated with some of these files. The code for this is found below. The problem is that none of the excel files have programmatic access to macros enabled. I can switch this manually on local copies of the file, but I currently only have read access to the directory of files. Is there any way I can temporarily change the programmatic access setting inside my code (to read the VBA code, not make any changes) without having write permission?
Also, I only know how to make the change to programmatic access manually (through the settings in each excel file). Seeing as I may eventually just need to get read/write access, is there any way I can do this in a batch process to save a lot of time manually opening and closing files?
VBA.VBProject project = WorkBook.VBProject;
VBA.VBComponents VBComponents = project.VBComponents;
string projectName = project.Name;
VBA.vbext_ProcKind procedureType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc;
VBA.VBComponent vbFunction;
foreach (Excel.Worksheet sheet in VBComponents)
{
vbFunction = sheet as VBA.VBComponent;
if (vbFunction != null)
{
VBA.CodeModule componentCode = vbFunction.CodeModule;
int componentCodeLines = componentCode.CountOfLines;
int line = 1;
while (line < componentCodeLines)
{
//EXAMINE LINE
line++;
}
}
}
.
EDIT:
The exact error message that is produced is "COMException was unhandled - Programmatic access to Visual Basic Project is not trusted".
I have since found that I get a different error message if I open one of the read-only files and change the setting. I cannot save the file, but if I leave it open, when it reaches the first .xlsm file, it prints the error message "COMException was unhandled - can't perform the operation since the project is protected".