0

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kf dhivya
  • 85
  • 1
  • 1
  • 10

2 Answers2

3

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
0m3r
  • 12,286
  • 15
  • 35
  • 71
0

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78