How to password protect an excel file through Matlab?
Show older comments
I am trying to password protect an Excel file which is created using Matlab..
clc;clear;dFlag_Excel = 0;
xlsfile = 'Try_V1.xlsx';
password = 'Test';
Excel = actxserver('Excel.Application'); % open Excel as a COM Automation server
Workbooks = Excel.Workbooks;
Workbook = Workbooks.Open([pwd,'\',xlsfile]);
Workbook.Protect(password, 'True', 'True');
But I am getting an error in this last line above in the script:
??? Invoke Error, Dispatch Exception: The parameter is incorrect.
Error in ==> Untitled at 7
Workbook.Protect('Test@123', 'True', 'True')
1 Comment
Ehtisham
on 28 Jun 2021
Go to the above link and download the simple code. It is easy to use and I have created it myself because I was facing the same problem as you and the code provided by anyone else did not serve my purpose. So I created the code myself.
Accepted Answer
More Answers (2)
Nicolas B.
on 11 Nov 2019
0 votes
2 Comments
Partha Mitra
on 11 Nov 2019
Ehtisham
on 28 Jun 2021
Go to the above link and download the simple code. It is easy to use and I have created it myself because I was facing the same problem as you and the code provided by anyone else did not serve my purpose. So I created the code myself.
Roofus Milton
on 22 Nov 2019
You are passing 'True' as a char when it should be logical/boolean.
Workbook.Protect('Test@123', true, true)
4 Comments
Partha Mitra
on 22 Nov 2019
Edited: Partha Mitra
on 22 Nov 2019
Roofus Milton
on 22 Nov 2019
Edited: Roofus Milton
on 22 Nov 2019
Your previous comment indicated the error moved from line 7 to line 18. I don't have your code so I am not sure what would contribute to the new error.
The code below sets the Password property of the workbook. This is separate from the Protect method.
filePath = "C:\Users\************.xlsx"
openPassword = "OpenPassword";
protectPassword = "ProtectPassword";
% attach to open Excel instance
excel = actxGetRunningServer('Excel.Application');
% ensure Excel is visible
excel.Visible = true;
% add a new workbook to the workbooks collection
wb = excel.Workbooks.Add();
% set the password property
wb.Password = openPassword;
% set ws variable to the active worksheet in wb
ws = wb.ActiveSheet();
% test data
ws.Range("A1").Value2 = "Roofus";
ws.Range("B1").Value2 = "Milton";
ws.Range("C1").Value2 = "Bear";
% protect the sheet
wb.Protect(protectPassword, true, true);
% save the file
wb.SaveAs(filePath);
% close the file
wb.Close();
Image Analyst
on 22 Nov 2019
This code works for me. It does ask for the password upon reopening the file from disk.
Ehtisham
on 28 Jun 2021
Go to the above link and download the simple code. It is easy to use and I have created it myself because I was facing the same problem as you and the code provided by anyone else did not serve my purpose. So I created the code myself.
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





