6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

outlookで受信したメールの添付ファイルを自動解凍する(パスワード付zip対応)

Last updated at Posted at 2015-01-12
  • outlookで受信したメールに添付されているzipファイルを任意のフォルダに格納する。
  • 格納したzipファイルを解凍する(パスワード付の場合は予め決められたパスワードを使う)
  • zipファイル解凍には7zipのdllを用いる。

事前準備

64bit OS (Windows8等)

32bit OS (Windows XX)

コード

saveZip.vba

Option Explicit

Private Declare Function SevenZip Lib "7-zip32.DLL" ( _
    ByVal hWnd As Long, _
    ByVal szCmdLine As String, _
    ByVal szOutput As String, _
    ByVal dwSize As Long) As Long
 
Const SAVE_PATH = "C:\mail_attach\"

Public Sub CustomRule_SaveFile(Item As Outlook.MailItem)
    Dim SaveStatus

    SaveStatus = SaveAttachFile(Item.Attachments)

End Sub

Function SaveAttachFile(Attachments As Outlook.Attachments)
    Dim UnzipStatus
    Dim sh As Object
    Dim ItemNumber, i As Long
    ItemNumber = Attachments.Count


    For i = 1 To ItemNumber
        Attachments.Item(i).SaveAsFile (SAVE_PATH & Attachments.Item(i).DisplayName)
        UnzipStatus = ExtractZIP(SAVE_PATH, SAVE_PATH & Attachments.Item(i).DisplayName, "test")
        
        
    Next i
    
    SaveAttachFile = True
    
End Function


'ZIPファイルを解凍
'引数 sDstPath:解凍先のフォルダーのパス
'     sZIPFile:ZIPファイルのパス
'     sPassWord:パスワード 省略可
'返り値 成功したら True、失敗したらFalse
Public Function ExtractZIP(sDstPath As String, sZIPFile As String, Optional sPassWord As String = "") As Boolean
    Dim sCmd As String
    sCmd = "X -hide -aoa "
    If sPassWord <> "" Then sCmd = sCmd & "-P" & sPassWord & " "
    sCmd = sCmd & Q2(sZIPFile) & " -o" & Q2(sDstPath)
    
    Debug.Print (sCmd)
    
    ExtractZIP = DoSevenZip(sCmd) = 0
End Function
 
Private Function DoSevenZip(sCmd As String) As Long
    Dim sRet As String * 1024
    DoSevenZip = SevenZip(0, sCmd, sRet, 1024)
    If DoSevenZip <> 0 Then MsgBox (Left(sRet, InStr(sRet, vbNullChar) - 1))
End Function
 
Public Function Q2(ByVal Text As String) As String
    Q2 = """" & Replace(Text, """", """""") & """"
End Function

参考サイト

6
11
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
6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?