Auto Create new Task upon Current Task completion

Status
Not open for further replies.
change one of the if lines:

If TypeOf Item Is Outlook.TaskItem AND instr(1, item.categories, "Complete") = 0 Then

or

If Task.Status = olTaskComplete AND instr(1, task.categories, "Complete") = 0 Then

the first one kicks it out quicker if the task has the category. I'm not sure it really matters - so use the one that is less confusing or will be easier to understand weeks down the road when you look at it and think "WTF is this doing." :)

also, i'm pretty sure you need to save the task to set the category when you use code.
You have
Item.Categories = Item.Categories & ";Complete"
but not item.save


Michael: I don't know if you know the answer, but is there any benefit to using
Set Task = Item
instead of referring to the original task as item in this macro?
 
The benefit is Task is declared as a certain type (TaskItem). That's called "early binding", it allows the IDE to list all the object's methods and properties as soon as you type the dot. If a variable is jusst declared as Object, the compiler knows only at run-time what type of object it'll be, so it cannot list any methods at design time. In order to get the methods listed, click Tools/Options and check "Automatically list items" (or similar). I don't remember if it's checked by default.

There's another way to get the methods listed: Say, you have entered
Code:
Task.Save
and want to display the item instead of saving it, but you don't remember the methods name. Simply place the cursor in the method (Save), then press ctrl+k.

Early-binding is also faster than late-binding, but in this scenario that shouldn't matter.

And, yes, after changing the original item by adding a new category it must be saved, of course, as Diane suggested. In the latest draft of the code it didn't matter because he also skipped to check for the newly added category.

As to your code above,
Code:
If TypeOf Item Is Outlook.TaskItem AND instr(1, item.categories, "Complete") = 0 Then
This one is true for every task item that has not the string "Complete" in its Categories property. Whereas
Code:
If Task.Status = olTaskComplete AND instr(1, task.categories, "Complete") = 0 Then
this one is true only for a task item, that's marked complete and has not the string "Complete" in its Categories property.
 
change one of the if lines:

If TypeOf Item Is Outlook.TaskItem AND instr(1, item.categories, "Complete") = 0 Then

or

If Task.Status = olTaskComplete AND instr(1, task.categories, "Complete") = 0 Then

the first one kicks it out quicker if the task has the category. I'm not sure it really matters - so use the one that is less confusing or will be easier to understand weeks down the road when you look at it and think "WTF is this doing." :)

also, i'm pretty sure you need to save the task to set the category when you use code.
You have
Item.Categories = Item.Categories & ";Complete"
but not item.save


Michael: I don't know if you know the answer, but is there any benefit to using
Set Task = Item
instead of referring to the original task as item in this macro?


I am now getting confused. Some of the most recent posts seem to reference a category of complete. My code relates purely to a complete status.
 
And your code doesn't work, so we are trying to get it working. That multiple new tasks are being created should mean that ItemChange is called multiple times for the same item. So you need any mechanism to identify that, and create a new task only once.
 
That multiple new tasks are being created should mean that ItemChange is called multiple times for the same item. So you need any mechanism to identify that, and create a new task only once.

Which is why you (s, not michael :)) add a category when it's marked complete then check for the category before creating a new task - if the category exists, a replacement task was created for it already.

Using a category is the most reliable method to identify an object that the code has touched. (Well, next to using the entryid. See later message.)

Make sure you have the first line - private withevents. It's missing from some of your examples.

task-test.png

This works here -

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

    ' See if task is completed and not yet processed
    If Task.Status = olTaskComplete And InStr(1, Item.Categories, "Complete") = 0 Then
    ' MsgBox "passed 1"
      If InStr(1, LCase$(Task.Categories), LCase$(CategoryName)) = 0 Then
   
     ' add the completed category and get that out of the way
        Task.Categories = Task.Categories & ";Complete"
        Task.Save '

      ' Create new task
        Set Task = Application.CreateItem(olTaskItem)
        With Task
   
        .StartDate = Item.DateCompleted
        .DueDate = Item.DateCompleted + 10
        .Status = olTaskInProgress
        .Categories = "Assessment"
        .Importance = olImportanceHigh   'can be olImportanceNormal, olImportanceHigh or olImportanceLow
        .Subject = Item.Subject
        .Body = Item.Body
        .Save ' use .Send .Save .Display
        End With
   
      End If
 
    End If
  End If
End Sub
 
Thanks Michael & Dianne,

I really appreciate the help and I thank you for the clarification regarding the use of the "complete" category. Regardless of my own reading and research I have learnt far more from you here on the forum.

Dianne, The "Private WithEvents" line is definitely there. It's just all the way at the start of my VBAProject.OTM as I have quite a bit of other code running within it as well.
 
Ah - every time I'd copy your code to test it, the withevents line was missing - just wanted to check.

On the 3 events with exchange problem - i understand 2 but 3 has me shaking my head. I removed the completed category and used .Subject = Item.Subject & time so i could see when the tasks were created. #2 was about 20 seconds after #1 and #3 was 2 seconds after #2. If #2 was created instantly (within 2 seconds), #3 wasn't.

My best guess is outlook makes the second during the update process - the macro sees the change before outlook completes the sync. Sync is about every 20 seconds, and if you do it close to the sync time, the item is immediately updated, so only two. Should be able to avoid it using the entry id, although I did not test it. (Maybe I'll test that next - I had another person with the same problem with appointments. )
 
Ok - this seems to work (as it should because there is only 1 item with that entry id)

in place of the set task = item line, use

Dim strID As String
strID = Item.EntryID
Set Task = Application.Session.GetItemFromID(strID)

(I should update my comments to include entryid as the other way to identify items already touched. :))
 
Great Diane,

I will implement that new code. Ironically after tweak my code with your previous update I have nit had a single duplicate all day today, although I've probably just been fortunate.

I have also successfully updated it with extra code so that only certain categories create the new task when marked complete to suit my particular requirements.

Once again thanks for the support. I feel that I have learned a lot and am really developing a passion for outlook coding to customise and streamline my business processes.
 
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