Share via


How to: Set and Clear Workbook Passwords

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

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.

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 _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 Visual Studio Tools for Office Projects

The Variable missing and Optional Parameters in Office Solutions

Change History

Date

History

Reason

July 2008

Added a code example that can be used in an application-level add-in.

Customer feedback.