Lock VBProject for Viewing using Code

  • Thread starter Nessy via OfficeKB.com
  • Start date
N

Nessy via OfficeKB.com

After reading a number of threads on this website, I have managed to define a
password for the VBProject in Workbook, WB, by making use of the following
code. However, I would also like to automatically set the option 'Lock
project for viewing' to True by code. Can anyone help, what do I need to do?

Public Sub ProtectCodeModule(ByVal WB As Workbook, Password as string)

Dim VBP As VBProject, oWin As VBIDE.Window
Dim wbActive As Workbook
Dim i As Integer

Set VBP = WB.VBProject
Set wbActive = WB

If VBP.Protection <> vbext_pp_none Then Exit Sub

Application.ScreenUpdating = False

' Close any code windows to ensure we hit the right project
For Each oWin In VBP.VBE.Windows
If InStr(oWin.Caption, "(") > 0 Then oWin.Close
Next oWin

WB.Activate
' Now use SendKeys to write password
Application.OnKey "%{F11}"
SendKeys "%{F11}%TE" & Password & "~~%{F11}", True

End Sub

Thanks in anticipation.
 
P

Peter T

This looks like an example of Bill Manville's that has been plagiarized on
whatever site you found it.
Application.OnKey "%{F11}"
I think you can comment the above but change the following
SendKeys "%{F11}%TE" & Password & "~~%{F11}", True
to
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password & "~"

Regards
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top