0 out of 1 rated this helpful - Rate this topic

How to: Set and Clear Workbook Passwords

Create a password to restrict access to a workbook. The following examples set the workbook's password. To clear the password, set the password to an empty string.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2007 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

To set a password

  • Set the password property of ThisWorkbook to a string provided by the user.

    
    private void SetPassword() 
    {
        string password = this.Application.InputBox("Enter the new password:",
            missing, missing, missing, missing, missing, missing, missing).ToString();
    
        string confirmPassword = this.Application.InputBox("Confirm the password:", 
            missing, missing, missing, missing, missing, missing, missing).ToString(); 
    
        if (password != confirmPassword)
        {
            MessageBox.Show("The passwords you typed do not match.");
            Globals.ThisWorkbook.Password = "";
        }
        else
        {
            Globals.ThisWorkbook.Password = password;
        } 
    }
    
    
    

To set a password for the active workbook

  • Set the Password property of the Microsoft.Office.Interop.Excel._Workbook class to a string provided by the user. To use this example, run the code from the ThisAddIn class in your project.

    
    private void SetPassword()
    {
        string password = this.Application.InputBox("Enter the new password:",
            missing, missing, missing, missing, missing, missing, missing).ToString();
    
        string confirmPassword = this.Application.InputBox("Confirm the password:",
            missing, missing, missing, missing, missing, missing, missing).ToString();
    
        if (password != confirmPassword)
        {
            System.Windows.Forms.MessageBox.Show
                ("The passwords you typed do not match.");
            this.Application.ActiveWorkbook.Password = "";
        }
        else
        {
            this.Application.ActiveWorkbook.Password = password;
        }
    }
    
    
    
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
"this"
I would like to know who is "this", I need to access to Application,

Regards