Code covered by the BSD License  

Highlights from
Unprotect Excel sheet

Unprotect Excel sheet

by

 

unprotecting a password protected sheet in excel, function requires a password to do that.

ProtecStatus2(Excel, password)
function ProtecStatus2(Excel, password)

answer = password;
for i = 1:Excel.Worksheets.Count
    sheet = get(Excel.Worksheets,'Item',i);
    invoke(sheet, 'Activate');
    if sheet.ProtectContents
        try
            invoke(sheet, 'Unprotect', answer);
        catch ME
            condition = 1;j = 1;
            while condition && j<4
                prompt = {'Enter Password to unlock worksheets:'};
                dlg_title = 'Sheet Protection Input';
                num_lines = 1;
                def = {''};
                answer = inputdlg(prompt,dlg_title,num_lines,def);
                try
                    condition = 0;
                    invoke(Excel.ActiveSheet, 'UnProtect', answer);
                catch
                    condition = 1;
                end
                j = j+1;
            end
            %quit excel if more than 3 attemts in unlocking a sheet is
            %performed.
            if j == 4
                errordlg('Sheet is Protected. Unable to proceed!');
                Excel.DisplayAlerts = 0;
                invoke(Excel,'Quit');
                delete(Excel);
                error('ERROR:Sheet is Protected. Unable to proceed!');
            end
        end
    end
end
invoke(Excel.Worksheets.Item(1), 'Activate');

Contact us