[ACCEPTED]-how to check Excel Workbook or sheet is password protected or not?-vsto

Accepted answer
Score: 12

You can check if a workbook is password 35 protected via the Workbook.HasPassword property. You can set 34 the workbook password via the Workbook.SaveAs method:

Excel.Workbook myWorkbook = ...;

if (!myWorkbook.HasPassword)
{
   excelWorkbook.Application.DisplayAlerts = false;

   excelWorkbook.SaveAs(
        excelWorkbook.Name,
        Type.Missing,
        "My Password",
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing);
}

A 33 worksheet can have it's cell contents protected, the 32 drawing objects protected, and/or the scenarios 31 protected. These can be checked via the 30 Worksheet.ProtectContents, Worksheet.ProtectDrawingObjects, and Worsksheet.ProtectScenarios properties, respectively. I do 29 not know of any way of testing if the worksheet 28 is protected with a password or not, other 27 than trying to call Worksheet.Unprotect, passing in an empty 26 string for the password, and then seeing 25 if the worksheet was successfully unprotected:

bool wasPasswordProtected;

try
{
    myWorksheet.Unprotect(string.Empty);

    // Unprotect suceeded:
    wasPasswordProtected = false;  
}
catch
{
    // Unprotect failed:
    wasPasswordProtected = true;
}

You 24 can set the protection setting for the worksheet 23 via the Worksheet.Protect method. The following example will 22 protect the worksheet if any of the three 21 protection elements are not set. It also 20 sets a password and passes in the 'UserInterfaceOnly' argument 19 as 'true', which means that only the user 18 is blocked from editing the worksheet, while 17 code such as VBA, VB.NET, or C# would not 16 be prevented from manipulating the worksheet. Setting 15 'UserInterfaceOnly' to 'false' would lock 14 out all changes, whether made by the user 13 or via code.

if(!myWorksheet.ProtectContents ||
   !myWorksheet.ProtectDrawinngObjects ||
   !myWorsksheet.ProtectScenarios)
{
    string myPassword = "...";

    bool protectContents = true;
    bool protectDrawingObjects = true;
    bool protectScenarios = true;

    bool userInterfaceOnly = true;

    myWorksheet.Protect(
        myPassword,
        protectDrawingObjects,
        protectContents, 
        protectScenarios,
        userInterfaceOnly,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing);

Note that the 'UserInterfaceOnly' does 12 NOT persist when the workbook is saved and 11 reverts to 'false' automatically when the 10 workbook is closed. If you wish it to be 9 'true' in all future sessions as well, the 8 'UserInterfaceOnly' setting must be re-applied 7 by calling the 'Worksheet.Protect' method 6 each time the workbook is open. This can 5 be be done by subscribing to the Workbook.Open event.

You 4 also might want to read the help files regarding 3 the Worksheet.Protect method so you can understand the optional 2 parameters, paying particular attention 1 to the 'UserInterfaceOnly' parameter.

Score: 3

Check the HasPassword property.

0

Score: 2

If you want to check if an excel workbook 8 is password protected checking the HasPassword 7 property on a workbook object will not work, because 6 you must open a workbook before you can 5 check the properties, and you can't open 4 a workbook that is password protected if 3 you don't have the password. See the problem?

This 2 is the solution I found to know if the workbook 1 has a password or not:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlsApp = new Excel.Application();
xlsApp.DisplayAlerts = false;

Excel.Workbooks wkbs = xlsApp.Workbooks;
Excel.Workbook wkb;


try
{
    wkb = wkbs.Open(path, ReadOnly: true, Password: "");
    //If you don't send a string for the password, it will popup a window
    //asking for the password and halt your program. If the workbook has no
    //password, it will open just fine.

}
catch (Exception ex)
{
    //If the file is password protected or otherwise unreadable, it will throw an exception.
}


wkb.Close(false);
xlsApp.Quit();

More Related questions