方法 : ブックのパスワードを設定およびクリアする
ブックへのアクセスを制限するパスワードを作成します。 次の例では、ブックのパスワードを設定します。 パスワードを消去するには、パスワードを空の文字列に設定します。
対象: このトピックの情報は、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; } }