How to pass through a VBA Project Password and remove the VBA Project Password via vb code

30 Views Asked by At

I have an excel spreadsheet with an "export" button, so that users can save a copy of the some of the sheets locally. The process I have written in vba also deletes some of the modules in the copied version. This worked fine, until I locked the VB Project with a Password. How can I get around this? I know the VB Project password and so ideally, I'd like to write code to remove the VB project password in the copied version, but I don't know how to do this?

The copied version still needs to be an xlsm file because there is code in some of the sheets that needs to be left in there, which doesn't need to be VB Protected.

All of the below code works fine. It only stopped working when I password protected the VB Project, and now I get the message "can't perform this operation since the project is protected"

Please can someone help provide the code I need to write to remove the password from the VB Project?

Dim FileName, FilePath As String
Dim wb_other As Workbook

FileName = "My Spreadsheet " & Format(Now(), "yyyy.mm.dd") & ".xlsm"
FilePath = "C:\Users\Downloads\"

ActiveWorkbook.SaveCopyAs ("" & FilePath & FileName & "")

Set wb_other = Workbooks.Open("" & FilePath & FileName & "")

wb_other.Sheets("View 1").Delete
wb_other.Sheets("View 2").Delete
wb_other.Queries("Query1").Delete

'Using Reference: Microsoft Visual Basic for Applications Extensibility 5.3
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Set VBProj = wb_other.VBProject
'Remove Module
    Set VBComp = VBProj.VBComponents("Main")
    VBProj.VBComponents.Remove VBComp
    Set VBProj = wb_other.VBProject
'Remove Module
    Set VBComp = VBProj.VBComponents("Export")
    VBProj.VBComponents.Remove VBComp
'Remove all code from ThisWorkbook code module
    VBProj.VBComponents("ThisWorkbook").CodeModule.DeleteLines 1, wb_other.VBProject.VBComponents("ThisWorkbook").CodeModule.CountOfLines
'Remove Form
    Set VBComp = VBProj.VBComponents("UserForm")
    VBProj.VBComponents.Remove VBComp

wb_other.Save
wb_other.Close
0

There are 0 best solutions below