共用方式為


HOW TO:設定和清除活頁簿密碼

更新: 2008 年 7 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Excel 2003

  • Excel 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

請建立密碼以限制對活頁簿的存取。下列範例會設定活頁簿的密碼。若要清除密碼,請將密碼設定為空字串。

透過文件層級自訂設定密碼

若要設定密碼

  • 將 ThisWorkbook 的密碼屬性設定為使用者提供的字串。

    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;
        } 
    }
    

透過應用程式層級增益集設定密碼

若要設定現用活頁簿的密碼

  • Microsoft.Office.Interop.Excel._Workbook 類別的 Password 屬性設定為使用者提供的字串。若要使用這個範例,請從專案的 ThisAddIn 類別中執行程式碼。

    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;
        }
    }
    

請參閱

工作

HOW TO:保護活頁簿

HOW TO:保護工作表

概念

使用活頁簿

Office 文件上的密碼保護

全域存取 Visual Studio Tools for Office 專案中的物件

了解 Office 方案中的選擇性參數

變更記錄

日期

記錄

原因

2008 年 7 月

加入可以用於應用程式層級增益集的程式碼範例。

客戶回函。