I want to write a function that will unprotect a VBA project for a workbook that's open in the same instance as the workboook that has this function. I'm trying to adapt Siddharth Rout's code in this answer, but I can't figure out how to launch the specific VBA Properties window for the VBProject that I'm interested in unlocking.
As a test, I'm trying to iterate through the VBProjects in the instance to see which one corresponds to the desired workbook, and then open the VBA Properties window for that project:
Sub findVBProj()
Dim prj As VBProject
Dim correctPrj As VBProject
For Each prj In Application.VBE.VBProjects
If prj.filename = "C:\Users\xyz\Desktop\testUnlock.xlsm" Then
Set correctPrj = prj
End If
Next
correctPrj.VBE.CommandBars(1).FindControl(id:=2578, recursive:=True).Execute
End Sub
But this doesn't launch the VBA Properties window for correctPrj
and therefore trigger the password prompt; instead, it opens the properties window for the project containing the module that contains findVBProj()
, which makes sense because that module is selected when I run findVBProj()
. How do I make the properties window for correctPrj
open?