How to password protect an excel file through Matlab?
Mostrar comentarios más antiguos
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 comentario
Ehtisham
el 28 de Jun. de 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.
Respuesta aceptada
Más respuestas (2)
Nicolas B.
el 11 de Nov. de 2019
0 votos
2 comentarios
Partha Mitra
el 11 de Nov. de 2019
Ehtisham
el 28 de Jun. de 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
el 22 de Nov. de 2019
You are passing 'True' as a char when it should be logical/boolean.
Workbook.Protect('Test@123', true, true)
4 comentarios
Partha Mitra
el 22 de Nov. de 2019
Editada: Partha Mitra
el 22 de Nov. de 2019
Roofus Milton
el 22 de Nov. de 2019
Editada: Roofus Milton
el 22 de Nov. de 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
el 22 de Nov. de 2019
This code works for me. It does ask for the password upon reopening the file from disk.
Ehtisham
el 28 de Jun. de 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.
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






