从Outlook邮件中提取压缩文件

时间:2017-02-27 04:20:30

标签: vba outlook outlook-vba

我正在尝试从Outlook邮件中提取zip文件。

下面是我的脚本,但它抛出一个错误对象变量或没有设置块变量。

开启

oApp.NameSpace((FileNameFolder)).CopyHere oApp.NameSpace((Atchmt.FileName)).Items

我如何解决它。

Sub Unzip()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Atchmt As Attachment
Dim FileName As String
Dim msg As Outlook.MailItem
Dim FileNameFolder As String
Dim FSO As Object               'variables for unzipping
Dim oApp As Object

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("TEST")

For Each msg In SubFolder.Items
   If msg.UnRead = True Then
        If LCase(msg.Subject) Like "*motor*" Then
            For Each Atchmt In msg.Attachments
                If (Right(Atchmt.FileName, 3) = "zip") Then
                MsgBox "1"
                        FileNameFolder = "C:\Users\xxxx\Documents\test\"
                        Set oApp = CreateObject("Shell.Application")
                        oApp.NameSpace((FileNameFolder)).CopyHere oApp.NameSpace((Atchmt.FileName)).Items

                End If
            Next
        End If
    End If
Next
End Sub

2 个答案:

答案 0 :(得分:2)

首先尝试保存zip文件,然后将其解压缩,如果您想要 delete 压缩文件,请尝试 Kill (zippath & zipname )

    Dim TempFile As String

    For Each msg In SubFolder.Items
       If msg.UnRead = True Then
            If LCase(msg.Subject) Like "*motor*" Then
                For Each Atchmt In msg.Attachments
                    If (Right(Atchmt.FileName, 3) = "zip") Then
'                    MsgBox "1"
                        FileNameFolder = "C:\Temp\Folders\"

                        Debug.Print FileNameFolder ' Immediate Window
                        Debug.Print Atchmt.FileName ' Immediate Window

                        TempFile = FileNameFolder & Atchmt.FileName

                        Atchmt.SaveAsFile TempFile

                        Set oApp = CreateObject("Shell.Application")
                        oApp.NameSpace((FileNameFolder)).CopyHere oApp.NameSpace((Atchmt.FileName)).Items

                        Kill (TempFile)

                    End If
                Next
            End If
        End If
    Next

答案 1 :(得分:0)

将msg声明为通用对象 - 您可以在收件箱中包含MailItem以外的对象,例如ReportItemMeetingItem