LoginSignup
0
0

More than 5 years have passed since last update.

ブックを保護するパスワード入力画面を表示するマクロ

Last updated at Posted at 2016-02-21

Public Const MESSAGE_OF_PASSWORD_INPUT_TITLE As String = "Input Box"
Public Const MESSAGE_OF_PASSWORD_INPUT_CONTENT As String = "Please input password"

Public Sub protectBookMain()
    Dim passPhase As String

    passPhase = InputBox(MESSAGE_OF_PASSWORD_INPUT_CONTENT, MESSAGE_OF_PASSWORD_INPUT_TITLE)

    If passPhase <> "" Then
        Call protectBook(passPhase)
    End If

End Sub

Public Sub protectBook(passPhase As String)

    Dim savePath As String

    Application.DisplayAlerts = False
    With ActiveWorkbook
            savePath = .Path
            saveFileName = .Name
            .SaveAs _
            Filename:=savePath & "\" & saveFileName, _
            passWord:=passPhase
    End With
    Application.DisplayAlerts = True

End Sub

ショートカットキー、およびイベントプロシージャの登録。

' オープン
Private Sub Workbook_Open()
    ' キーバインドの設定
    Application.OnKey "^{m}", "protectBookMain"
End Sub

' 閉じる前
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call protectBookMain
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0