2
votes

I found this code on another website. It should take an email found in an Outlook folder and unzip the attachment. It uses a temporary location to do this.

I am using Outlook 2013 and the references I am using are: Visual Basic for Application, Microsoft Outlook 15.0 Object Library, OLE Automation, Microsoft Office 15.0 Object Library. I am running this code currently in a module.

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

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

    For Each msg In SubFolder.Items
        For Each Atchmt In msg.Attachments
            If (Right(Atchmt.FileName, 3) = "zip") Then
                FileNameFolder = Environ("USERPROFILE") & "Documents\"
                Set oApp = CreateObject("Shell.Application")
                oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items
            End If
        Next
    Next
End Sub

I am getting an error "Object variable or With block variable not set" on this line.

oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items
3
Your missing Atchmt.SaveAsFile FileNameFolder after FileNameFolder = Environ("USERPROFILE") & "Documents\"0m3r

3 Answers

1
votes

I had to change

DIm oAPP as Object

to

Dim oApp as Shell

Early-binding using 'Microsoft Shell Controls and Automation'. Not sure why it was complaining about the late-binding

0
votes

If we are talking about the Namespace class from the Outlook object mode, it doesn't provide the CopyHere method. The object itself provides methods for logging in and out, accessing storage objects directly by ID, accessing certain special default folders directly, and accessing data sources owned by other users. Use GetNamespace("MAPI") to return the Outlook NameSpace object from the Application object.

The CopyHere method copies an item or items to a folder. The item or items to copy should be passed as a parameter. This can be a string that represents a file name, a FolderItem object, or a FolderItems object. But not the attachment filename. You need to save the attachment on the disk first.

0
votes

Make sure Sub Folder ("Books") in your Email Inbox Exist & "\Documents\Files\" that's where unzip files will be saved

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

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

    For Each msg In SubFolder.Items
        For Each Atchmt In msg.Attachments
            If (Right(Atchmt.FileName, 3) = "zip") Then
                FileNameFolder = Environ("USERPROFILE") & "\Documents\Files\"
                Atchmt.SaveAsFile FileNameFolder 
                Set oApp = CreateObject("Shell.Application")
                oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items
            End If
        Next
    Next
End Sub