MATLAB Answers

0

How to password protect an excel file through Matlab?

Asked by Partha Mitra on 11 Nov 2019 at 4:44
Latest activity Commented on by Roofus Milton on 23 Nov 2019 at 17:18
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')

  0 Comments

Sign in to comment.

Products


Release

R2010b

3 Answers

Answer by Image Analyst
on 22 Nov 2019 at 15:18
 Accepted Answer

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.

  8 Comments

Thanks for your help! it works perfectly.
Thanks Image Analyst and thanks Milton!
Here are the steps to cycle through protected states.
Local Variables
existingFilePath = "C:\*********.xlsx";
filePath = "C:\*********.xlsx";
openPassword = "OpenPassword";
wbProtectPassword = "Workbook";
wsProtectPassword = "Worksheet";
Create Excel Objects
% attach to open Excel instance
excel = actxGetRunningServer('Excel.Application');
% ensure Excel is visible
excel.Visible = true;
% check to see if we create a new workbook or open existing
if(strcmp(existingFilePath, ""))
% add a new workbook to the workbooks collection
wb = excel.Workbooks.Add();
else
% open existing file, note [] facilitates optional parameters
wb = excel.Workbooks.Open(existingFilePath, [], [], [], openPassword);
end
% set the active worksheet
ws = wb.ActiveSheet();
Workbook
Protect the Workbook
% protect the sheet
wb.Protect(wbProtectPassword, true, true);
% set the password property
wb.Password = openPassword;
Test Workbook Protection
If ProtectStructure returns a logical 1, the code below will produce an error: "Add method of Sheets class failed".
% check if the workbook structure is protected
wb.ProtectStructure
% add a worksheet to the workbook
ws2 = wb.Sheets.Add();
% rename the worksheet
ws2.Name = "TestSheet";
Unprotect the Workbook
% set the password to an empty string removes password requirement
wb.Password = "";
% unprotects the workbook structure
wb.Unprotect(wbProtectPassword);
Worksheet
Protect the Worksheet
% check to see if we created a new workbook, only write data if we create a
% new workbook
if(strcmp(existingFilePath, ""))
% write test data
ws.Range("A1").Value2 = "Roofus";
ws.Range("B1").Value2 = "Milton";
ws.Range("C1").Value2 = "Bear";
end
% set worksheet level protection
ws.Protect(wsProtectPassword, true, true);
Test Worksheet protection
If the ProtectContents property returns a logical 1 then the code will produce an error.
% get the protected status
ws.ProtectContents
% write test data
ws.Range("A2").Value2 = "Test1";
Unprotect the Worksheet
% call the unprotect method
ws.Unprotect(wsProtectPassword);
% write test data
ws.Range("B2").Value2 = "Test2";
Cleanup
% save the file
wb.SaveAs(filePath);
% close the file
wb.Close();

Sign in to comment.


Answer by Nicolas B.
on 11 Nov 2019 at 7:34

I recommand you to use the xls_protect_sheets() from MATLAB exchange. It will be easier for you.

  1 Comment

This works but it only protects the sheet.
What I want is to protect the file completely.
I don't want others to open the file without a password.

Sign in to comment.


Answer by Roofus Milton on 22 Nov 2019 at 2:06

You are passing 'True' as a char when it should be logical/boolean.
Workbook.Protect('Test@123', true, true)

  3 Comments

Hi Milton,
I tried as per your suggestion but the excel file is still unprotected.
I tried using 2 ways :
1. As suggested by you:
Workbook.Protect('Test@123', true, true)
2. Excel.ActiveWorkbook.Protect('Test@123', true, true);
There is no popup for any error in MATLAB. But also the excel file remains unprotected.
Am I missing some settings which I need to do?
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();
This code works for me. It does ask for the password upon reopening the file from disk.

Sign in to comment.