Visual Basic auto create task from email including attachments

Not open for further replies.


Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
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
Thank you. What code would i need to add to assign the task to a category filtered by the subject line.
you mean "if subject = A, then category = A" "if subject = B, then category = B"?
Yes thats what i want the code to do.
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
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?
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
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"
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.
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
M How to Create Macro in Visual Basic to add Contacts from Personal Folder Using Outlook 4
E trust access to visual basic project greyed out in outlook 2003 Outlook VBA and Custom Forms 1
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
S Visual indicator of a certain property or to show a macro toggle Outlook VBA and Custom Forms 2
K Visual of Tasks on desktop Using Outlook 2
D Outlook AddIn can't load outside Visual Studio Outlook VBA and Custom Forms 1
T Deploying add-ins from Visual Studio Tools for Office Outlook VBA and Custom Forms 2
U Outlook Addin Creation using Visual Studio 2008 Outlook VBA and Custom Forms 1
W OL giving basic auth when logged in to Win as specific user Using Outlook 0
CWM330 " Basic Auth" Using accounts in Outlook 0
e_a_g_l_e_p_i Is there a way to add something that is in the "Format Text" tab to the "basic Text" on the message tab Using Outlook 1
Raewyn Basic accounting software? Using Outlook 2
A Basic BCM question about sync to Outlook 2013 BCM (Business Contact Manager) 1
D Basic pst file quesstion Using Outlook 1
D Vista Home Basic / Contact Manager 2003 BCM (Business Contact Manager) 1
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
D Auto Remove [EXTERNAL] from Subject + ConversationTopic (WITHOUT Redemption plug-in) Outlook VBA and Custom Forms 2
P Email address auto-completes work fine on laptop, but no longer on desktop Using Outlook 3
C New pc, new outlook, is it possible to import auto-complete emailaddress Using Outlook 5
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 4
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
F Auto changing email subject line in bulk Using Outlook 2
T Outlook 2019 Not Using Auto Compete After Deletion of 365 Using Outlook 1
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
nmanikrishnan Auto-reply from default account Using Outlook 1
A Imap account not auto syncing inbox at startup Using Outlook 0
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S Auto forward for multiple emails Outlook VBA and Custom Forms 0
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
V Auto-complete stopped working Using Outlook 4
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
R Auto Forwarding with different "From" Outlook VBA and Custom Forms 0
P auto-complete is hopelessly broken Using Outlook 0
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
V Auto-Submitted: auto-replied in header Using Outlook 0
R Auto display of new email does not work on non-default account Outlook VBA and Custom Forms 0
B Outlook 2016 Auto-archive creates new folder Using Outlook 3
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
P Auto assign shared mailbox Outlook VBA and Custom Forms 1
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
P [SOLVED] Auto remove [EXTERNAL] from subject Using Outlook 16
Z Add text to auto-forwarded e-mail Outlook VBA and Custom Forms 4

Similar threads