How to password protect an excel file through Matlab?
29 ビュー (過去 30 日間)
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')
Image Analyst 2019 年 11 月 22 日
Have you checked out the Excel API for workbooks and worksheets: https://docs.microsoft.com/en-us/office/vba/api/Excel.Worksheet.Protect
From what I can see from the left panel, the worksheet object has a Protect() method but a workbook object does not. However, the text is ambiguous. It says
A string that specifies a case-sensitive password for the worksheet or workbook. If this argument is omitted, you can unprotect the worksheet or workbook without using a password. Otherwise, you must specify the password to unprotect the worksheet or workbook. If you forget the password, you cannot unprotect the worksheet or workbook.
Yet up at the top, and in the left panel it seems to say only worksheets, not workbooks. Perhaps it means that if a workbook has only one worksheet then protecting the worksheet will also protect the workbook, however it doesn't say that explicitly. If I get time, I'll play around with it later.