+ Reply to Thread
Results 1 to 2 of 2

Unprotecting VBE Project to Make Code Changes

  1. #1
    Kaisies
    Guest

    Unprotecting VBE Project to Make Code Changes

    I found this code that uses sendkeys for unlocking/locking your VBE Project :

    Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
    Dim vbProj As Object

    Set vbProj = WB.VBProject

    'can't do it if already unlocked!
    If vbProj.Protection <> 1 Then Exit Sub

    Set Application.VBE.ActiveVBProject = vbProj

    ' now use lovely SendKeys to quote the project password
    SendKeys Password & "~~"

    Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
    End Sub

    Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
    Dim vbProj As Object

    Set vbProj = WB.VBProject

    'can't do it if already locked!
    If vbProj.Protection = 1 Then Exit Sub

    Set Application.VBE.ActiveVBProject = vbProj

    ' now use lovely SendKeys to set the project password


    SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password & "~"

    Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute

    WB.Save
    End Sub


    Also modifying Chip's information on removing/adding Subs from Projects, I
    created this Sub:

    Sub DeleteCodeLine(WB As Workbook, ModuleName, StartLine)
    Dim VBCodeMod As CodeModule

    Set VBCodeMod = WB.VBProject.VBComponents(ModuleName).CodeModule
    With VBCodeMod
    .DeleteLines StartLine, 1
    End With

    End Sub



    I then have a commandbutton that Does this then: (where sheetname is the
    other workbook I want to unprotect / reprotect


    UnprotectVBProject Workbooks(sheetname), "abc"
    DeleteCodeLine Workbooks(sheetname), "sheet28", 2
    ProtectVBProject Workbooks(sheetname), "abc"


    When I click the commandbutton it gives me an error:

    Run-time error '50289':

    Can't perform operation since the project is protected.


    I know I'm doing something stupid wrong, but, I can't figure out what...
    anyone care to take a stab at it?

    Thanks!

    -Kaisies

  2. #2
    Kaisies
    Guest

    RE: Unprotecting VBE Project to Make Code Changes

    The error occurs on the:

    Set VBCodeMod = WB.VBProject.VBComponents(ModuleName).CodeModule

    line of code in the Sub DeleteCodeLine

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1