Copy code from a protected VBA project

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
I have this procedure to copy lines of code from a different workbook to the existing workbook, however the workbook being copied from has the vba project protected.
Is there any way around this? Unprotected the project is not an option.

Perhaps its possible to put these lines of code into a string in the code and write that into the existing workbook instead of copying a complete module?

Note the code works fine when I manually unprotect the project but I cannot have it unprotected for other users


Popup when trying to copy the module:

1634832808500.png


VBA Code:
Sub copycode()
'copies code from RP macro wrkbk thisworkbook code module to the active workbook
    Dim WB As Workbook
    Dim MWB As Workbook
    Dim WBCodeMod1 As Object, WBCodeMod2 As Object

    Set WB = ActiveWorkbook
    Windows("RP Macro Wrkbk.xlsb").Activate
    Set MWB = ActiveWorkbook
    ActiveWindow.Visible = False
    ' Copy the Workbook level Event handlers
    
    Set WBCodeMod1 = MWB.VBProject.VBComponents("ThisWorkbook").CodeModule
    Set WBCodeMod2 = WB.VBProject.VBComponents("ThisWorkbook").CodeModule
    WBCodeMod2.InsertLines 1, WBCodeMod1.Lines(1, WBCodeMod1.countoflines)
    
    
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Your request is very confusing.

You want to copy code from a workbook that is protected ... yet you say you can unprotect that workbook.
If you can unprotect the workbook ... what is the issue you are facing ?

My only thought is to included in the macro you posted, code to unprotect the other workbook first, copy the code, then re-protect the workbook.

?????
 
Upvote 0
Your request is very confusing.

You want to copy code from a workbook that is protected ... yet you say you can unprotect that workbook.
If you can unprotect the workbook ... what is the issue you are facing ?

My only thought is to included in the macro you posted, code to unprotect the other workbook first, copy the code, then re-protect the workbook.

?????
I don't have code to unprotect and reprotect the workbook, I just manually enter the password
such code would solve my issue
 
Upvote 0
I tested this version here. It works. I hesitated to provide this one because the first version was more direct in
its application. Paste this into a regular module. You can use Command Buttons to activate or de-activate or you
can use the Workbook_Open and Workbook_BeforeClose commands.

VBA Code:
Option Explicit

Sub LockVBAProject()
     With Application
           '//execute the controls to lock the project\\
           .VBE.CommandBars("Menu Bar").Controls("Tools") _
                       .Controls("VBAProject Properties...").Execute
           '//activate 'protection'\\
           .SendKeys "^{TAB}"
           '//CAUTION: this either checks OR UNchecks the\\
           '//"Lock Project for Viewing" checkbox, if it's already\\
           '//been locked for viewing, then this will UNlock it\\
           .SendKeys "{ }"
           '//enter password (password is 123 in this example)\\
           .SendKeys "{TAB}" & "123"
           '//confirm password\\
           .SendKeys "{TAB}" & "123"
           '//scroll down to OK key\\
           .SendKeys "{TAB}"
           '//click OK key\\
           .SendKeys "{ENTER}"
           'the project is now locked - this takes effect
           'the very next time the book's opened...
     End With
     Application.DisplayAlerts = False
     ThisWorkbook.Save
     ActiveWorkbook.Close
    
End Sub

Sub UnLockAndViewVBAProject()
     With Application
           '//we execute the "VBAProject Properties..." control\\
           '//just to invoke the password dialog, the password\\
           '//must be given before the dialog can be shown :) \\
           .SendKeys "123"
           .SendKeys "{ENTER}"
           '//now reset the project password and relock it so\\
           '//that it's locked again when the workbook's closed\\
           .VBE.CommandBars("Menu Bar").Controls("Tools") _
                       .Controls("VBAProject Properties...").Execute
           .SendKeys "^{TAB}"
           .SendKeys "{TAB}" & "123"
           .SendKeys "{TAB}" & "123"
           .SendKeys "{TAB}"
           .SendKeys "{ENTER}"
     End With
End Sub
 
Upvote 0
It still didn't work.
I think I will save the code in a new workbook, and I'll copy the code from there instead of the workbook that has all other code being used till now.
That won't have to be protected since it will only contain the code that needs to get copied.
 
Upvote 0
runtime error 5 invalid procedure call or argument by .VBE.CommandBars("Menu Bar").Controls("Tools") .Controls("VBAProject Properties...")).Execute
 
Upvote 0
Do you have the following REFERENCES selected in TOOLS ?
 

Attachments

  • VBE Code.jpg
    VBE Code.jpg
    65.9 KB · Views: 40
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top