Outlook Script to Download Attatchements RRS feed

  • Question

  • I have a scrip to download attatchments that I am tying to a rule. The problem is that I am recieving a object error on line 6 which i cant figure out:

    Any help would be greatly appreciated!

    Public Sub SaveTaskSummary(MItem As Outlook.MailItem)
    Dim oAttachment As Outlook.Attachment
    Dim sSaveFolder As String
        sSaveFolder = "C:\Users\bwelsh\Desktop\FulfillmentScorecards\Daily Task Summary Reports\"
    Dim dateFormat As String
        dateFormat = Format(Mitm.ReceivedTime, "yyyy-mm-dd hh:mm:ss")
    For Each oAttachment In MItem.Attachments
        oAttachment.SaveAsFile sSaveFolder & dateFormat & oAttachment.DisplayName
    End Sub

    Thursday, May 18, 2017 5:04 PM

All replies

  • Consider this.

    Private Sub GetAttachments()
        Dim ns As Namespace
        Dim Inbox As Outlook.MAPIFolder
        Dim Item As Object
        Dim Atmt As Outlook.Attachment
        Dim FileName As String
        Set ns = GetNamespace("MAPI")
        Set Inbox = ns.Folders("MailboxName").Folders("Inbox")
        If Inbox.Items.Count = 0 Then
            MsgBox "There are no messages in the Inbox.", vbInformation, _
                    "Nothing Found"
            Exit Sub
        End If
        For Each Item In Inbox.Items
            For Each Atmt In Item.Attachments
                If Atmt.Type = 1 And InStr(Atmt, "xlsx") > 0 Then
                    FileName = "C:\attachments\" & Atmt.FileName
                    Atmt.SaveAsFile FileName
                End If
            Next Atmt
        Next Item
    End Sub

    Set a reference to MS Outlook and remember, the "MailboxName" is your email address.


    Monday, July 10, 2017 3:41 AM
  • The error in your code is the typo in the object name

    dateFormat = Format(Mitm.ReceivedTime, "yyyy-mm-dd hh:mm:ss")

    The object name is MItem not Mitm.

    You will then get another error relating to the filename. the Colon : in the dateformat is an illegal filename character. "yyyy-mm-dd hh:mm:ss" Change the switch to avoid the colons.

    Graham Mayor - Word MVP

    Monday, July 10, 2017 4:50 AM