如何:设置和清除工作簿密码
更新: 2008 年 7 月
适用于 |
---|
本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型
Microsoft 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; } }
请参见
任务
概念
对 Visual Studio Tools for Office 项目中的对象的全局访问
修订记录
日期 |
修订历史记录 |
原因 |
---|---|---|
2008 年 7 月 |
增加了一个可在应用程序级外接程序中使用的代码示例。 |
客户反馈。 |