Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to use C# to get all the VBA code from an Excel work book.

Here is a code snippet:

Excel.Workbooks xlsWorkbooks;
Excel.Workbook xlsWorkBook;
Excel.Worksheet xlsWorkSheet;

VBIDE.VBE vbEditor;
VBIDE.VBProject vbProject;
           

var xlsApplication = new Excel.Application();
var path = @"C:\VBATest.xls"; 

xlsWorkBook = xlsApplication.Workbooks.Open(path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

var oModule = xlsWorkBook.VBProject;// <-- error occurs here



When I try to use var oModule = xlsWorkBook.VBProject;
I get a com exception and am told 'Programmatic access to Visual Basic Project is not trusted'.

I have opened the excel spreadsheet, under tools -> macro _> security, trusted publishers, and checked the 'Trust access to Visual Basic' project. This has not helped. In fact, it seems to un-check itself once I run the program.

I don't know if it makes any difference, but my Microsoft Excel Object Reference is 11.5 and not 12.0 as on the tutorial I was using.

Any help would be precipitated :doh:
Posted
Comments
E.F. Nijboer 15-Jul-10 4:30am    
I'm not sure, but I know some of the security stuff is magically done by IE. So, what happens if you lower the protection in IE?

Maybe this link also helps: http://support.microsoft.com/default.aspx?scid=kb;en-us;827742

1 solution

There might be a registry key that you can modify. Otherwise, you'd use Excel without applying SP3 and possibly SP2.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900