Excel VBA | 実務用 VBA コード集(テンプレ付き)

Excel VBA VBA
スポンサーリンク

VBA拡張版:ZIPファイルのパスワードを自動生成して管理者に別メールで通知

これまで「パスワード付きZIPを送信」する方法を紹介しました。今回はさらに拡張して、パスワードを自動生成し、管理者に別メールで通知するテンプレートです。これにより、セキュリティを強化しつつ、受信者に安全にパスワードを伝えられます。


手順の流れ

  1. ランダムなパスワードを自動生成(英数字+記号など)
  2. 7-Zipでパスワード付きZIPを作成
  3. ZIPファイルをメール添付して送信
  4. 別メールで管理者にパスワードを通知

サンプルコード

Option Explicit

'=== ランダムパスワード生成関数 ===
Function GeneratePassword(Optional length As Integer = 12) As String
    Dim chars As String, i As Integer, result As String
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
    Randomize
    For i = 1 To length
        result = result & Mid(chars, Int(Rnd() * Len(chars)) + 1, 1)
    Next i
    GeneratePassword = result
End Function

Sub SendSecureZipWithPasswordNotice()
    Dim zipFilePath As String, filesToZip As Variant
    Dim password As String, sevenZipPath As String, cmd As String
    
    '=== ZIPファイルパス ===
    zipFilePath = ThisWorkbook.Path & "\secure_report.zip"
    
    '=== 対象ファイル ===
    filesToZip = Array( _
        ThisWorkbook.Path & "\Sales.xlsx", _
        ThisWorkbook.Path & "\Error.xlsx", _
        ThisWorkbook.Path & "\Stock.xlsx")
    
    '=== 自動生成パスワード ===
    password = GeneratePassword(12)
    
    '=== 7-Zipのパス(環境に合わせて変更) ===
    sevenZipPath = """C:\Program Files\7-Zip\7z.exe"""
    
    '=== コマンド生成 ===
    cmd = sevenZipPath & " a -tzip """ & zipFilePath & """ -p" & password & " -y"
    
    Dim i As Integer
    For i = LBound(filesToZip) To UBound(filesToZip)
        cmd = cmd & " """ & filesToZip(i) & """"
    Next i
    
    '=== ZIP作成実行 ===
    Shell cmd, vbHide
    
    '=== ZIPファイル送信 ===
    Call SendMailWithAttachment("admin@domain.com", _
        "【VBAタスク通知】セキュアZIPレポート", _
        "<h2>🔒 セキュアZIPレポート</h2><p>パスワード付きZIPを添付しました。</p>", _
        zipFilePath)
    
    '=== パスワード通知メール(別メール) ===
    Call SendMailWithAttachment("security@domain.com", _
        "【VBAタスク通知】ZIPパスワード通知", _
        "<h2>🔑 ZIPパスワード通知</h2><p>今回のパスワードは以下です:</p><p><b>" & password & "</b></p>", _
        "")
    
    MsgBox "パスワード付きZIPと通知メールを送信しました", vbInformation
End Sub

'=== 汎用メール送信関数 ===
Sub SendMailWithAttachment(toAddress As String, subject As String, htmlBody As String, Optional attachmentPath As String = "")
    Dim objMsg As Object, objConf As Object
    Set objMsg = CreateObject("CDO.Message")
    Set objConf = CreateObject("CDO.Configuration")
    
    ' SMTP設定(例:Office365)
    With objConf.Fields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_account@domain.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
        .Update
    End With
    
    With objMsg
        Set .Configuration = objConf
        .From = "your_account@domain.com"
        .To = toAddress
        .Subject = subject
        .HTMLBody = htmlBody
        If attachmentPath <> "" Then .AddAttachment attachmentPath
        .Send
    End With
End Sub
VB

この拡張版のポイント

  • ランダムパスワード生成 → 毎回異なる強力なパスワードを自動作成
  • ZIP送信とパスワード通知を分離 → セキュリティ強化(パスワードは別経路で通知)
  • 汎用メール送信関数 → 添付あり/なし両方に対応

実務でのメリット

  • セキュリティ強化 → パスワードを別メールで通知することで漏洩リスクを低減
  • 完全自動化可能 → タスクスケジューラと組み合わせれば「毎月のセキュアZIPレポート+パスワード通知」を自動送信
  • 柔軟性 → 管理者やセキュリティ担当にパスワードを安全に共有可能

✅ まとめ

  • ZIPファイルに自動生成パスワードを設定
  • ZIP送信とパスワード通知を別メールで分離
  • セキュリティ強化された自動レポート配信が完成
タイトルとURLをコピーしました