Compartir a través de


How to: Programmatically remove protection from worksheets

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can programmatically remove protection from a Microsoft Office Excel worksheet.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

The following example uses the variable getPasswordFromUser, which contains a password obtained from the user.

To unprotect a worksheet in a document-level customization

  1. Call the Unprotect method of the worksheet and pass in the password, if necessary. This example assumes that you are working with a worksheet named Sheet1.

    Globals.Sheet1.Unprotect(getPasswordFromUser);
    
    Globals.Sheet1.Unprotect(getPasswordFromUser)
    

To unprotect a worksheet in a VSTO Add-in

  1. Call the Unprotect method of the active worksheet and pass in the password, if necessary.

    ((Excel.Worksheet)Application.ActiveSheet).Unprotect(getPasswordFromUser);
    
    CType(Application.ActiveSheet, Excel.Worksheet).Unprotect(getPasswordFromUser)
    

See also