Visual Basic auto create task from email including attachments

Discussion in 'Using Outlook' started by carol1, Oct 12, 2012.

  1. carol1


    I would like the email attachment to be included in the task. What would i need to add to the code below:

    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    objTask.Subject = Item.Subject
    objTask.StartDate = Item.ReceivedTime
    objTask.Body = Item.Body
    Set objTask = Nothing

    End Sub
  2. Diane Poremsky

    Diane Poremsky

    Senior Member
    Add these two lines to the code:
    Dim newAttachment As Outlook.Attachment
    Set newAttachment = objTask.Attachments.Add(Item, Outlook.OlAttachmentType.olEmbeddeditem)
  3. carol1


    Thank you. What code would i need to add to assign the task to a category filtered by the subject line.
  4. Diane Poremsky

    Diane Poremsky

    Senior Member
    you mean "if subject = A, then category = A" "if subject = B, then category = B"?
  5. carol1


    Yes thats what i want the code to do.
  6. Diane Poremsky

    Diane Poremsky

    Senior Member
    You'd use the following lines in somewhere between the Set objtask = application and lines.

    If InStr(Item.Subject, "keyword") Then objTask.Categories = "keyword"

    If InStr(Item.Subject, "keyword2") Then objTask.Categories = "keyword2"

    You could split it to make it easier to manage - the three lines at the top, the objtask line before

    Dim strCat As String

    If InStr(Item.Subject, "keyword") Then strCat = "keyword"

    If InStr(Item.Subject, "more") Then strCat = "more"
    objTask.Categories = strCat

    Either of the above works for a few keywords, but if you need to use a lot, you need to use an array.

    At the top:

    Dim strCat As String

    Dim arrCat As Variant

    arrCat = Array("1keyword", "2keyword", "3keyword", "4keyword", "5keyword", "6keyword", "7keyword", "8keyword", "9keyword")

    For i = LBound(arrCat) To UBound(arrCat)
    If InStr(Item.Subject, arrCat(i)) Then strCat = arrCat(i)

    Next i
    before save:
    objTask.Categories = strCat
  7. carol1


    I used the first code from your reply and it did not work for me. I'm not sure what I'm doing wrong. Would i be able to assign a category by the sender?
  8. Forum Admin

    Forum Admin

    Senior Member
    Do you get any error messages?

    InStr should be case-insensitive, but try using LCase(Item.subject) instead of Item.Subject and lower case keywords.

    Yes, you can use any field -
    If Item.SenderEmailAddress = "" Then objTask.Categories = "keyword"

    Note that this will not work for addresses on your exchange server -they don't have a smtp address.

    BTW - if you want the message to remain unread in the Inbox, add the following before the end of the macro.
    Item.UnRead = True
  9. carol1


    No error messages it just didnt assign the task to the category. My subject line will always start with [Ticket # then the rest of the subject line will vary. I used the following in my code as my keyword "[Ticket"
  10. Diane Poremsky

    Diane Poremsky

    Senior Member
    First test it with a simple category on every task it creates -

    objTask.categories = "Test"

    if that works, try

    If left(item.subject, 9) = "[Ticket #" Then objTask.Categories = "Ticket"

    ETA: put the code right before the command.

Share This Page