Protect/Unprotect VBAProject Properties
Find bottlenecks in your Excel workbooks
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: Protect/Unprotect VBAProject Properties

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Winnipeg, Manitoba, CANADA
    Posts
    144
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    My Excel program is protected, but I have a problem. I have to Unprotect the project when I do a certain task and then I would like to re-protect it from unwanted eyes/fingers. Can I do this in code? I tried this and it doesn't seem to work...

    ' We have to unprotect the project
    VBAProject.ThisWorkbook.Unprotect password:="test123"

    ' do some stuff here

    ' Now re-protect the project
    VBAProject.ThisWorkbook.Protect password:="test123"
    Thanx.

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Well, never done/tried this, but, one question came to mind, WHY ??

    I don't understand what you're trying to do...
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  3. #3
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Well, I've been looking for some property/method/event, or something, but I can't seem to find a way to do that... which would seem logical to me, to avoid creation of crack subs directly in VBA (Yes, yes, I know there are many programs that can do that, but no of them is built in VBA, as far as I know...)

    One, not very good way and don't know how reliable, would be using SendKeys.
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  4. #4
    Board Regular
    Join Date
    Feb 2002
    Location
    Winnipeg, Manitoba, CANADA
    Posts
    144
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    WHY? Good question...

    I am creating and removing userforms on the fly. You can't create a userform if the Project is protected! I've used SendKeys before where would there be an example?
    Thanx.

  5. #5
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Found this on Google.

    http://groups.google.com/groups?q=*e...ome.com&rnum=2

    I saw what you meant after I posted my first response...
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  6. #6
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Here is a method I used some time ago
    that protects and unprotects with a paasword.

    Option Explicit

    Const BreakIt As String = "%{F11}%TE+{TAB}{RIGHT}%V{+}{TAB}"

    Sub Change_VBA_PW()
    Dim WB As Workbook
    Dim Password As String

    Set WB = ActiveWorkbook
    Password = "ivan"
    Call SetVBProjectPassword(WB, Password)

    End Sub

    Sub SetVBProjectPassword(WB As Workbook, ByVal Password As String)
    'Needs reference to Visual Basic for Applications Extensibility Library
    Dim VBP As VBProject
    Dim OpenWin As VBIDE.Window
    Dim i As Integer

    Set VBP = WB.VBProject

    Application.ScreenUpdating = False

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

    WB.Activate

    SendKeys BreakIt & Password & "{tab}" & Password & "~" & "%{F11}~", True
    'SendKeys "enter", True
    WB.Activate
    SendKeys "%{F11}", True
    Application.ScreenUpdating = True
    End Sub




    Ivan

  7. #7
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Very nice code Ivan !
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  8. #8
    Board Regular
    Join Date
    Feb 2002
    Location
    Winnipeg, Manitoba, CANADA
    Posts
    144
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Looks good, thanx for your help and everyone else. Cheers.
    Thanx.

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com