Outlook VBA script to save file with subject as filename

Discussion in 'Programming' started by Xyseven, Nov 1, 2012.

  1. Xyseven

    Xyseven Junior Member

    Nov 1, 2012
    Likes Received:
    Good day. New member here :)

    Long story short:
    I would like a script that grabs the attachments from a incoming mails and saves them in a specified folder with the subject as the filename.

    The bigger picture:
    I regularly recieve mails with attached pdf files. The pdf filename is always called "Vedlegg" ("attachement" in norwegian) and the subject field is always "ARBEIDSORDRE 589492/2012 ,VH35682 ,FORHÅNDSVIS" except for the numbers being different each time. I work in a car workshop and these pdf's are work-orders containing a description of what to repair on the different cars. Thus the numbers will be different on each mail, but I want them to be automatically stored with the subject field as the filename.

    Also, I am aware that "/" can not be used in filenames. So I would like something that inserts a space instead and also maybe deletes the first and last word "ARBEIDSORDRE" and "FORHÅNDSVIS" as those are of no interest when I later want to search for it.

    Kind Regards
    #1 Xyseven, Nov 1, 2012
    Last edited: Nov 1, 2012
  2. Ken g6

    Ken g6 Programming Moderator, Elite Member

    Dec 11, 1999
    Likes Received:
    OK, so what programming languages do you know that might have a POP3 or IMAP library?

    Edit: I always miss the title. :$
    #2 Ken g6, Nov 3, 2012
    Last edited: Nov 3, 2012
  3. coloumb

    coloumb Diamond Member

    Oct 9, 1999
    Likes Received:
  4. Xyseven

    Xyseven Junior Member

    Nov 1, 2012
    Likes Received:
    Thank you for the quick reply. I have no knowledge of VBA or any programming at all. My job is with automobilenetworking and hardware :) But I will try that forum you linked - thanks!

    I am using this today (which does almost what I want):

    Sub SaveAllAttachments(objitem As MailItem)

    Dim objAttachments As Outlook.Attachments
    Dim strName, strLocation As String
    Dim dblCount, dblLoop As Double
    strLocation = "K:\Ny mappe struktur\Servicemarked\Volkswagen og Audi verksted\Gjennomganger - Teksteark\Jack\"

    On Error GoTo ExitSub
    If objitem.Class = olMail Then
    Set objAttachments = objitem.Attachments
    dblCount = objAttachments.Count
    If dblCount <= 0 Then
    GoTo 100
    End If
    For dblLoop = 1 To dblCount
    strID = " from " & Format(Date, "mm-dd-yy")
    strName = objAttachments.Item(dblLoop).Filename 'Get attachment name
    strExt = Right$(strName, 4) 'Store file Extension
    strName = Left$(strName, Len(strName) - 4) 'Remove file Extension
    strName = strName & strID & strExt 'Reattach Extension
    strName = strLocation & strName
    objAttachments.Item(dblLoop).SaveAsFile strName
    Next dblLoop
    End If
    Set objAttachments = Nothing
    Set objOutlook = Nothing

    End Sub