Auto Create new Task upon Current Task completion

Status
Not open for further replies.

sjbtax

New Member
Outlook version
Email Account
I am wanting to create some vba code that will automatically create a new task when an existing task is completed. I need it to auto-run (via the VbaProject.OTM) so that when tasks of certain categories are marked as complete a new task is created using the same subject, a different category, and using the completion date as the start date of the new task with a due date 10 days later.

Is this at all possible. I have tried using snippets of other code and modifying it to suit but so far seem to be going absolutely nowhere.
 
Thanks Michael,
That's actually the code that I was trying to use as a starting point, but I couldn't seem to get it to work as is much less modify it to do what I want. I'll keep researching and trying.
 
Basically, you need to replace the lines that set the category and save the task with the code to create a new task.

Code:
If Task.Status = olTaskComplete Then
      If LCase$(Task.Categories) <> LCase$(CategoryName) Then
        Task.Categories = CategoryName
        Task.Save
      End If
    End If

It's an Application_Startup macro, so it goes in ThisOutlookSession and for testing changes without restarting Outlook, click in Application_Startup and click Run. Make sure macro security is set to low until you are finished with it, then sign it with selfcert.
 
I am still having problems getting this to work.

Here is my code:

Code:
' <DieseOutlookSitzung>

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderTasks).Items
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
  On Error Resume Next
  Dim Task As Outlook.TaskItem
  Dim DueDate As String
  Const CategoryName As String = "Assessment"

  If TypeOf Item Is Outlook.TaskItem Then
    Set Task = Item
    Task.Save
    If Task.Status = olTaskComplete Then
      If LCase$(Task.Categories) <> LCase$(CategoryName) Then
        Application.CreateItem (olTaskItem)
        Task.StartDate = Item.Start
        Task.DueDate = Item.DueDate + 1
        Task.Status = olTaskInProgress
        Task.Categories = "Assessment"
        Task.Importance = olImportanceHigh   'can be olImportanceNormal, olImportanceHigh or olImportanceLow
        Task.Subject = Item.Subject
        Task.Body = Item.Body
        Task.Save ' use .Send to send it instead
      End If
    End If
  End If
End Sub
' </DieseOutlookSitzung>



Unfortunately this still just changes the original task, rather than retaining and marking it as complete and then creating a new task with new properties as specified.
 
Change this line
Application.CreateItem (olTaskItem)
to
Set Task=Application.CreateItem (olTaskItem)

The "Item" variable then still points to the edited item, while the "Task" variable will point to the new item returned by the CreateItem function.
 
Thanks Michael & Diane,

I now almost have the code working as hoped. Here is my updated code:

Code:
' <DieseOutlookSitzung>

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderTasks).Items
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
  On Error Resume Next
  Dim Task As Outlook.TaskItem
  Dim DueDate As String
  Const CategoryName As String = "Assessment"

  If TypeOf Item Is Outlook.TaskItem Then
    'Set Task = Item
    'Task.Save
    If Task.Status = olTaskComplete Then
      If LCase$(Task.Categories) <> LCase$(CategoryName) Then
        Set Task = Application.CreateItem(olTaskItem)
        Task.StartDate = Item.DateCompleted
        Task.DueDate = Item.DateCompleted + 10
        Task.Status = olTaskInProgress
        Task.Categories = "Assessment"
        Task.Importance = olImportanceHigh   'can be olImportanceNormal, olImportanceHigh or olImportanceLow
        Task.Subject = Item.Subject
        Task.Body = Item.Body
        Task.Save ' use .Send .Save .Display
      End If
    End If
  End If
End Sub
' </DieseOutlookSitzung>

The only problem now is it is creating three new tasks. So looks like I am continuing with more troubleshooting.
 
First, whenever a code doesn't do what you expect, remove the On Error Resume Next as it instructs to ignore errors, and that is nonsense if you don't know what the code does.

It'll then tell you that here
If Task.Status = olTaskComplete Then
the Task variable is nothing. That's because you commented this line out:
Set Task = Item
 
Oops. Thanks for the tip about the "On Error Resume" line. Very handy.

I had been commenting various lines out to see how it changed the outcome in an attempt to fix the issue with it creating three new tasks. I had obviously forgot to change it back before posting the code.

I have checked and the "Set Task = Item" line has definatley been made active and there are no error messages being advised. The code is somehow still generating three new identical tasks when an existing task is marked a complete.
 
I'm using this and getting just one new task - bu, if you are copying it as an identical task, why not just using the regenerate task feature?

Code:
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderTasks).Items
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
  'On Error Resume Next
  Dim Task As Outlook.TaskItem
  Dim DueDate As String
  Const CategoryName As String = "Assessment"

  If TypeOf Item Is Outlook.TaskItem Then
    'Set Task = Item
    'Task.Save
    If Item.Status = olTaskComplete Then
     ' If LCase$(Task.Categories) <> LCase$(CategoryName) Then
        Set Task = Application.CreateItem(olTaskItem)
        Task.StartDate = Item.DateCompleted
        Task.DueDate = Item.DateCompleted + 10
        Task.Status = olTaskInProgress
        Task.Categories = "Assessment"
        Task.Importance = olImportanceHigh   'can be olImportanceNormal, olImportanceHigh or olImportanceLow
        Task.Subject = Item.Subject
        Task.Body = Item.Body
        Task.Save ' use .Send .Save .Display
     ' End If
    End If
  End If
 
Thanks for testing. It's not an identical task that I am regenerating. It uses the date completed as the start date and sets a due date 10 days later. There will also be other changes and filters that will be made but I want to get the basic concept working first before I proceed further.

Further testing shows that when a new task is created and marked complete immediately then I do not get duplicate copies. Only when my existing task seems to be already in the task list with a different status does it seem to create duplicates when marked read. Interestingly I have had a some tasks only regenerate two copies, others will generate three. Could it have something to do with the fact that I am using outlook on exchange with my task list accessible on multiple computers?
 
Hmm. I'm getting 3 with an exchange account too. I'm guessing it's due to cached mode. The updated item syncs and that triggers the macro again for some unknown reason.

Add another if categories - check for Completed then at the end, add the completed category to the original task.

Code:
If Item.Status = olTaskComplete Then
 If Item.Status = olTaskComplete And InStr(1, Item.Categories, "Complete") = 0 Then

      If LCase$(Item.Categories) <> LCase$(CategoryName) Then
        ' do your stuff
      End If

        Item.Categories = Item.Categories & ";Complete"
        Item.Save

    End If
    End If
 
If you assign more categories, this
Code:
If LCase$(Item.Categories) <> LCase$(CategoryName) Then
doesn't work. Instead you could search with Instr, too.

If the issue is due to syncing, that is a procedure that calls itself, I'd try this approach first:
Code:
sub bla()
  on error goto err_handler
  static busy as boolean
  if busy=false then busy=true else exit sub
  'your stuff
  err_handler:
  busy=false
  if err then msgbox err.description
end sub
 
Thanks Diane & Michael,

You are a wealth of information and have been a great help, both for this project and general learning.

Diane, your solution seems to be working so far. I will proceed with my other adjustments.

Michael, I will keep your response in mind as there may be more categories involved down the track and I may need to implement this to combat any resulting issues.
 
Hmmmm. No I spoke too soon. There appears to have been a delay but between the creation of the first task but an extra two eventually appeared.
 
I haven't been able to figure out exactly where to put "busy flag" code into mine. I either get an error when i run it or it doesn't do anything.
 
My current code still looks like this:

Code:
Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderTasks).Items
End Sub



Private Sub Items_ItemChange(ByVal Item As Object)
  'On Error Resume Next
  Dim Task As Outlook.TaskItem
  Dim DueDate As String
  Const CategoryName As String = "Assessment"

  If TypeOf Item Is Outlook.TaskItem Then
    Set Task = Item
    'Task.Save
    If Task.Status = olTaskComplete Then
      If LCase$(Task.Categories) <> LCase$(CategoryName) Then
        Set Task = Application.CreateItem(olTaskItem)
        Task.StartDate = Item.DateCompleted
        Task.DueDate = Item.DateCompleted + 10
        Task.Status = olTaskInProgress
        Task.Categories = "Assessment"
        Task.Importance = olImportanceHigh   'can be olImportanceNormal, olImportanceHigh or olImportanceLow
        Task.Subject = Item.Subject
        Task.Body = Item.Body
        Item.Categories = Item.Categories & ";Complete"
        Task.Save ' use .Send .Save .Display
      End If
    End If
  End If
End Sub

I am still trying to figure out exactly where to place the "busy flag" code as everywhere I place it seems to result in an error.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
C Visual Basic auto create task from email including attachments Using Outlook 9
D create auto number field in task form Outlook VBA and Custom Forms 4
rc4524 Create auto follow-up reminder email for already sent messages Outlook VBA and Custom Forms 1
R Auto-create receipt from email and forward to payer Using Outlook 3
H Create/Apply auto formatting rules by VB? Outlook VBA and Custom Forms 2
G Macro: Create New Message and Auto populate To Field Outlook VBA and Custom Forms 5
S Create Auto eMail with Web table Content Outlook VBA and Custom Forms 1
P Email address auto-completes work fine on laptop, but no longer on desktop Using Outlook 2
C New pc, new outlook, is it possible to import auto-complete emailaddress Using Outlook 4
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 3
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
N Disable Auto Read Receipts sent after using Advanced Find Using Outlook 4
Q Prompt button to auto turn on Out of Office Outlook VBA and Custom Forms 3
P Auto Insert Current Date or Time into Email Subject Outlook VBA and Custom Forms 2
S Messages moved / deleted by auto-archive are not synchronized to exchange Exchange Server Administration 8
B Outlook 2010 is Auto Purging when not configured for that Using Outlook 1
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Auto Accept Meetings from the General Calendar Using Outlook 3
R auto send email when meeting closes from a shared calendar only Outlook VBA and Custom Forms 2
S auto-mapping mailboxes in outlook impacting an ost file? Exchange Server Administration 2
M Auto expand Distribution List Before Sending Email Outlook VBA and Custom Forms 1
M Auto-export mail to Excel Outlook VBA and Custom Forms 2
Ms_Cynic Auto-pasting email content in calendar appt? Using Outlook 2

Similar threads

Back
Top