Extract Zipped file from Outlook mail

时光毁灭记忆、已成空白 提交于 2019-12-12 04:35:02

问题


I'm trying to extract an zip file from my Outlook mail.

Below is my script but its throwing an error object variable or with block variable not set.

On

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

How I fix it.

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

回答1:


Try saving the zip file first then extract it, if you want to delete the zip file then try 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



回答2:


Declare msg as a generic Object - you can have objects other than MailItem in the Inbox, such as ReportItem or MeetingItem.



来源:https://stackoverflow.com/questions/42477789/extract-zipped-file-from-outlook-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!