How to password protect an excel file through Matlab?
古いコメントを表示
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 件のコメント
Ehtisham
2021 年 6 月 28 日
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.
採用された回答
その他の回答 (2 件)
Nicolas B.
2019 年 11 月 11 日
0 投票
2 件のコメント
Partha Mitra
2019 年 11 月 11 日
Ehtisham
2021 年 6 月 28 日
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
2019 年 11 月 22 日
You are passing 'True' as a char when it should be logical/boolean.
Workbook.Protect('Test@123', true, true)
4 件のコメント
Partha Mitra
2019 年 11 月 22 日
編集済み: Partha Mitra
2019 年 11 月 22 日
Roofus Milton
2019 年 11 月 22 日
編集済み: Roofus Milton
2019 年 11 月 22 日
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
2019 年 11 月 22 日
This code works for me. It does ask for the password upon reopening the file from disk.
Ehtisham
2021 年 6 月 28 日
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.
カテゴリ
ヘルプ センター および File Exchange で Use COM Objects in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






