At work we use VBA and currently they want to lock reports we generate with macros.
I\'ve been trying to lock a project automatically (given a password and workbook
Include a check to see if the project is already protected/locked, and don't attempt to lock it if it is already locked
If Workbooks(nameWorkbookForMarket).VBProject.Protection = 1 Then Exit Sub
You also want to ensure the correct project is active, so:
Set vbProj = Workbooks(nameWorkbookForMarket).VBProject
Set Application.VBE.ActiveVBProject = vbProj
If vbProj.Protection = 1 Then Exit Sub
' send keys
An alternative approach is to split the reports' process into two parts, keeping code in a separate workbook. It is unusual, in my view, that new reports/workbooks would continue to be created that include code.