Edit password protected macro in Workbook 1 with another macro in Workbook 2

31 views Asked by At

I am trying to unprotect a project to delete a macro. I have 131 such files where this macro needs to be removed. My issue is that I am not able to unprotect the project. I get Run-time error 438: Object doesn't support this property or method on the line "wb.VBProject.Unprotect password". I hope someone can assist me, please. It will be much appreciated!

Sub RemoveMacroInWorkbooks()
    Dim folderPath As String
    Dim fileName As String
    Dim wb As Workbook
    Dim vbComp As VBComponent
    Dim password As String

    ' Set the folder path where your workbooks are located
    folderPath = "C:\Users\DELL\Dropbox\Commodities"

    ' Set the password for the protected modules
    password = "password"

    ' Loop through each workbook in the folder
    fileName = Dir(folderPath & "\*.xlsm")
    Do While fileName <> ""
        ' Open the workbook with password
        Set wb = Workbooks.Open(folderPath & "\" & fileName, , , , password)

        ' Unprotect the VBA project if it is protected
        If wb.VBProject.Protection <> 0 Then ' 0 means no protection
            ' Provide the password to unprotect the VBA project
            wb.VBProject.Unprotect password
        End If

        ' Access the VBComponents of the workbook
        For Each vbComp In wb.VBProject.VBComponents
            ' Check if the VBComponent is a module
            If vbComp.Type = vbext_ct_StdModule Then
                ' Access the code module of the module
                With vbComp.codeModule
                    ' Find and delete the specified macro code
                    Dim startLine As Long
                    Dim endLine As Long
                    Dim i As Long

                    ' Find the starting line of the specified macro
                    startLine = 1
                    Do While startLine > 0
                        startLine = .Find("Sub navDashb", startLine, 1, -1, -1, False)
                        If startLine > 0 Then
                            ' Find the ending line of the specified macro
                            endLine = .Find("End Sub", startLine, 1, -1, -1, False)
                            If endLine > 0 Then
                                ' Delete the lines containing the specified macro
                                For i = endLine To startLine Step -1
                                    .DeleteLines i, 1
                                Next i
                            End If
                        End If
                    Loop
                End With
            End If
        Next vbComp

        ' Protect the VBA project again
        wb.VBProject.Protect password

        ' Save and close the workbook
        wb.Save
        wb.Close

        ' Get the next workbook in the folder
        fileName = Dir
    Loop
End Sub
0

There are 0 answers