HOW TO:設定和清除活頁簿密碼
請建立密碼以限制對活頁簿的存取。 下列範例會設定活頁簿的密碼。 若要清除密碼,請將密碼設定為空字串。
**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
透過文件層級自訂設定密碼
若要設定密碼
將 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; } }