PDA

View Full Version : Create Task from Email, and link to the email



mallycat
09-17-2011, 12:01 AM
I'm new here, and new to Outlook VBA. I have quite some experience with Excel VBA, but learnt most of my skills from the macro recorder + books. No macro recorder in Outlook 2010 makes it hard.

I am trying to create a task from an email, and place a link back to the original email in the task. I know how to copy the email body and subject ok, but that's not what I want to do. I want to put the subject of the email in the new task, and provide a link back to the orginal email - not copy the body of the email into the task.

I can't find any code that will do this. Hope someone can help.


Sub TaskFromEmail()
Dim myItem As MailItem
Dim olTsk As TaskItem

Set myItem = Outlook.Application.ActiveExplorer.Selection.item(1)
Set olTsk = Outlook.Application.CreateItem(olTaskItem)

With olTsk
.Subject = myItem.Subject
.StartDate = Now()
'[paste link to myItem here]
.Save
End With

Set olTsk = Nothing
Set myItem = Nothing
End Sub


If you go to the menu Insert\Outlook Item, I can manually do what I want, but I can't find out how to code this.

Matt

Charlize
09-20-2011, 02:43 AM
Not sure about the icon but this will insert a link to the selected message by using the entryID of the message. Beware that you can't move the message after the creation of a task (entryID is different each time you move to another folder).
Sub Create_link_in_task_to_mail()
'make sure it's a mailitem and nothing else (ie. read receipt or something else)
Dim mymessage As Outlook.MailItem
'this is for the taskitem
Dim mytask As Outlook.TaskItem
'gonna make a task from the selected mail
Set mymessage = ActiveExplorer.Selection.Item(1)
'we create a new task
Set mytask = CreateItem(olTaskItem)
'the body is just a link to the mailmessage
mytask.Body = Body & vbCrLf & "url:outlook (http://www.vbaexpress.com/forum/outlook):" & mymessage.EntryID & vbCrLf
'display the task
mytask.Display
End Sub
I think your coding would be something like :
Sub TaskFromEmail()
Dim myItem As Outlook.MailItem
Dim olTsk As Outlook.TaskItem

Set myItem = ActiveExplorer.Selection.Item(1)
Set olTsk = CreateItem(olTaskItem)

With olTsk
.Subject = myItem.Subject
.StartDate = Now()
'[paste link to myItem here]
.Body = Body & vbCrLf & "url:outlook (http://www.vbaexpress.com/forum/outlook)" & myItem.EntryID & vbCrLf
.Save
End With

Set olTsk = Nothing
Set myItem = Nothing
End SubCharlize

Charlize
09-20-2011, 10:57 AM
Little glitch in the coding ...
change .Body = Body & vbCrLf & "url:outlook" & myItem.EntryID & vbCrLfto
.Body = .Body & vbCrLf & "url:outlook:" & myItem.EntryID & vbCrLf
Sorry about that.

Charlize

ps.: It's a change in your coding.

jverbakel
01-13-2012, 04:23 PM
Sub TaakAanmakenVanMail()
' Joost Verbakel, 13 januari 2012
' Deze macro maakt van een geselecteerd mailbericht een taak en sluit in die taak als msg het originele mailbericht in.
' Vanuit de taak kun je de mail openen en evt. beantwoorden '

Dim objNS As Outlook.NameSpace
Set objNS = Application.GetNamespace("MAPI")

If objNS.GetDefaultFolder(olFolderInbox).Parent = "Postvak - Joost Verbakel" Then
Dim objItem As Outlook.MailItem
Dim objTask As Outlook.TaskItem
Dim NumOfDays As Integer
Dim DayToRemind As Date
Const attPath As String = "P:\personal\"

On Error Resume Next
Set objItem = Outlook.ActiveInspector.CurrentItem

If objItem Is Nothing Then
If (ActiveExplorer.Selection.Count = 1) And (ActiveExplorer.Selection.Item(1).Class = olMail) Then
Set objItem = ActiveExplorer.Selection.Item(1)
End If
End If
On Error GoTo 0

If objItem Is Nothing Then
MsgBox "De taak kan niet aangemaakt worden. De knop werkt alleen onder de volgende voorwaarden:" & vbCr & vbCr & _
"-- Je bekijkt een mail item." & vbCr & _
"-- Je staat in de Inbox en hebt 1 bericht geselecteerd.", vbInformation
GoTo ExitProc
End If

Set objTask = Outlook.CreateItem(olTaskItem)

DayToRemind = Date + 1

With objTask
.StartDate = DayToRemind
.Subject = "Herinnering voor opvolgen: " & objItem.Subject
'.Status = olTaskInProgress
.Importance = objItem.Importance
.DueDate = DayToRemind
.ReminderSet = True

' het originele mailbericht invoegen in de taak
' als eerste wordt mail gekopieerd
objItem.SaveAs attPath & objItem.EntryID

' dan wordt deze ingevoegd in de taak
objTask.Body = objItem.Body
objTask.Attachments.Add attPath & objItem.EntryID, olEmbeddeditem

' de bewaarde kopie wordt verwijderd
Kill (attPath & objItem.EntryID)

.Save
.Display

End With
End If

ExitProc:
Set objTask = Nothing
Set objItem = Nothing
Set objNS = Nothing

End Sub


I have for you a question.
Do you have the code for to make a calender appointment from a mailitem?

I Hope so.

mallycat
04-12-2012, 02:37 PM
Thanks for the responses here. I realise this was over 6 months ago - sorry I got distracted. The URL hyperlink approach works well and I will use that.

Thanks agian