remove password excel (vba)

Sub RemovePasswordFromExcel()
Dim excelApp As Object
Dim workbook As Object
Dim excelFilePath As String
Dim password As String

' Specify the path to your Excel file and the password
excelFilePath = "C:\Path\To\Your\ExcelFile.xlsx"
password = "YourPassword"

' Error handling setup
On Error GoTo ErrorHandler

' Create an instance of Excel
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = False ' Keep Excel invisible

' Suppress prompts and alerts
excelApp.DisplayAlerts = False

' Open the workbook with the password
Set workbook = excelApp.Workbooks.Open(Filename:=excelFilePath, Password:=password)

' Check if the workbook has a password
If workbook.HasPassword Then
    ' Remove the password by saving the workbook with no password
    workbook.Password = ""
    workbook.SaveAs Filename:=excelFilePath, Password:=""
    MsgBox "Password removed from the Excel file!"
Else
    MsgBox "The file does not have a password."
End If

' Close the workbook and quit Excel
workbook.Close False
excelApp.Quit

' Clean up
Set workbook = Nothing
Set excelApp = Nothing
Exit Sub

ErrorHandler:
MsgBox “An error occurred: ” & Err.Description
If Not excelApp Is Nothing Then
‘ Close the workbook if open
If Not workbook Is Nothing Then workbook.Close False
‘ Quit the Excel application
excelApp.Quit
End If
‘ Clean up
Set workbook = Nothing
Set excelApp = Nothing
End Sub

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *