Udostępnij za pośrednictwem


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.

Setting a Password in a Document-Level Customization

To set a password

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

    Private Sub SetPassword()
        Dim password As String
        Dim confirmPassword As String
    
        password = Me.Application.InputBox("Enter the new password:").ToString()
        confirmPassword = Me.Application.InputBox("Confirm the password:").ToString()
    
        If password <> confirmPassword Then
            MessageBox.Show("The passwords you typed do not match.")
            Globals.ThisWorkbook.Password = ""
        Else
            Globals.ThisWorkbook.Password = password
        End If
    End Sub
    
    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;
        } 
    }
    

Setting a Password in an Application-Level Add-In

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 Sub SetPassword()
        Dim password As String
        Dim confirmPassword As String
    
        password = Me.Application.InputBox("Enter the new password:").ToString()
        confirmPassword = Me.Application.InputBox("Confirm the password:").ToString()
    
        If password <> confirmPassword Then
            System.Windows.Forms.MessageBox.Show("The passwords you typed do not match.")
            Me.Application.ActiveWorkbook.Password = ""
        Else
            Me.Application.ActiveWorkbook.Password = password
        End If
    End Sub
    
    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;
        }
    }
    

See Also

Tasks

How to: Protect Workbooks

How to: Protect Worksheets

Concepts

Working with Workbooks

Password Protection on Office Documents

Global Access to Objects in Office Projects

Optional Parameters in Office Solutions