Locking a Project programmatically in VBA

前端 未结 1 593
余生分开走
余生分开走 2020-12-20 04:20

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

相关标签:
1条回答
  • 2020-12-20 05:13

    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.

    0 讨论(0)
提交回复
热议问题