Consulting

Results 1 to 5 of 5

Thread: Create Task from Email, and link to the email

  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    17
    Location

    Create Task from Email, and link to the email

    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.

    [vba]
    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
    [/vba]

    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

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,288
    Location
    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).
    [vba]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:" & mymessage.EntryID & vbCrLf
    'display the task
    mytask.Display
    End Sub[/vba]
    I think your coding would be something like :
    [vba]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" & myItem.EntryID & vbCrLf
    .Save
    End With

    Set olTsk = Nothing
    Set myItem = Nothing
    End Sub[/vba]Charlize

  3. #3
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,288
    Location
    Little glitch in the coding ...
    change [VBA].Body = Body & vbCrLf & "url:outlook" & myItem.EntryID & vbCrLf[/VBA]to
    [VBA].Body = .Body & vbCrLf & "url:outlook:" & myItem.EntryID & vbCrLf[/VBA]
    Sorry about that.

    Charlize

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

  4. #4

    another way

    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.

  5. #5
    VBAX Regular
    Joined
    Sep 2011
    Posts
    17
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •