Update Task Custom Field based upon Task Overdue Status

sjbtax

Member
Outlook version
Email Account
Operating system::    Windows 10
Outlook version:     Outlook 365
Email type or host:    Microsoft 365

I am trying to add some text to a custom field I have created when a task is overdue so that I can use it when sorting some of my lists.

I plan on running it automatically somehow at the start of the day and then hoping to apply it when a new task is added or changed for new or updated appointment during the day.

I have tried the following code, however even though it does not cause any errors, it does not place anything in my custom field "TaskStatus" either. Anyone have any ideas?

Code:
Sub UpdateTaskStatus()
    Dim objTask As Outlook.TaskItem
    Dim overdueDate As Date
    
    ' Define the overdue threshold (e.g., overdue if due date is in the past)
    overdueDate = Date
    
    On Error Resume Next ' Continue if an error occurs (e.g., not a task item)
    
    ' Check if the current item is a task item
    If Application.ActiveInspector.CurrentItem.Class = olTask Then
        Set objTask = Application.ActiveInspector.CurrentItem
        
        ' Check if the task is overdue
        If objTask.DueDate < overdueDate And objTask.Complete = False Then
            ' Update custom field "TaskStatus" with "Overdue"
            objTask.UserProperties("TaskStatus").Value = "Overdue"
            objTask.Save ' Save the changes
        End If
    End If
    
    Set objTask = Nothing ' Release the object reference
    On Error GoTo 0 ' Reset error handling
End Sub
 
inspector is open item. Use Application.ActiveExplorer.Selection.Item(1) for the selected item.

I have a function here that detects open or selected and uses it.


To see how to run it daily on all tasks, see

Watching for changed items uses ItemChange - you need to watch the folder for changes.

If running it on the folder a few times a day is good enough, a reminder that triggers a macro that runs on all items in the tasks folder might be easier.
 
inspector is open item. Use Application.ActiveExplorer.Selection.Item(1) for the selected item.

I have a function here that detects open or selected and uses it.


To see how to run it daily on all tasks, see

Watching for changed items uses ItemChange - you need to watch the folder for changes.

If running it on the folder a few times a day is good enough, a reminder that triggers a macro that runs on all items in the tasks folder might be easier.
Thanks Diane,

I'll check all that out.
 
inspector is open item. Use Application.ActiveExplorer.Selection.Item(1) for the selected item.

I have a function here that detects open or selected and uses it.


To see how to run it daily on all tasks, see

Watching for changed items uses ItemChange - you need to watch the folder for changes.

If running it on the folder a few times a day is good enough, a reminder that triggers a macro that runs on all items in the tasks folder might be easier.
In the meantime, I have added the macro I have above to a button in the quick access ribbon in the toolbar within open task windows. When I run the macro when the task window it should apply the text "Overdue" to my custom field "TaskStatus". Since this task is open then this should work, but does nothing. I feel I should get this working right before adding automation options to the code.
 
I feel I should get this working right before adding automation options to the code.
Yes, you should.

Does the field exist on the form? If not, you need to add it.

Set objProp = objTask.UserProperties.Add("TaskStatus", olText, True)
objTask.UserProperties("TaskStatus").Value = "Overdue"
objTask.Save ' Save the changes


If it exists on some but not all tasks, you need to use find -
From Create a custom field for Outlook messages

Code:
Set UserProp = objTask.UserProperties.Find("TaskStatus")
   If UserProp Is Nothing Then
      Set objProp = obj.UserProperties.Add("TaskStatus", olText, True)
End if
objTask.UserProperties("TaskStatus").Value = "Overdue"
        obj.Save
 
Yes, you should.

Does the field exist on the form? If not, you need to add it.

Set objProp = objTask.UserProperties.Add("TaskStatus", olText, True)
objTask.UserProperties("TaskStatus").Value = "Overdue"
objTask.Save ' Save the changes


If it exists on some but not all tasks, you need to use find -
From Create a custom field for Outlook messages

Code:
Set UserProp = objTask.UserProperties.Find("TaskStatus")
   If UserProp Is Nothing Then
      Set objProp = obj.UserProperties.Add("TaskStatus", olText, True)
End if
objTask.UserProperties("TaskStatus").Value = "Overdue"
        obj.Save
OK, so I now have:-
Code:
Sub UpdateTaskStatus()
    Dim objTask As Outlook.TaskItem
    Dim overdueDate As Date
    
    ' Define the overdue threshold (e.g., overdue if due date is in the past)
    overdueDate = Date
    
    On Error Resume Next ' Continue if an error occurs (e.g., not a task item)
    
    ' Check if the current item is a task item
    If Application.ActiveInspector.CurrentItem.Class = olTask Then
        Set objTask = Application.ActiveInspector.CurrentItem
        
        ' Check if the task is overdue
        If objTask.DueDate < overdueDate And objTask.Complete = False Then
            ' Update custom field "TaskStatus" with "Overdue"
            Set UserProp = objTask.UserProperties.Find("TaskStatus")
        If UserProp Is Nothing Then
           Set objProp = obj.UserProperties.Add("TaskStatus", olText, True)
        End If
            objTask.MyUserProperties("TaskStatus").Value = "Overdue"
            objTask.Save ' Save the changes
        End If
    End If
    
    Set objTask = Nothing ' Release the object reference
    On Error GoTo 0 ' Reset error handling
End Sub

But it is still not doing anything
 
Two typos. I forgot to change obj to objTask - not sure where the My came from, but removing that and it worked.

1723779007155.png


Try this code:
Code:
Sub UpdateTaskStatus()
    Dim objTask As Outlook.TaskItem
    Dim overdueDate As Date
    Dim objProp As Outlook.UserProperty
    
    ' Define the overdue threshold (e.g., overdue if due date is in the past)
    overdueDate = Date
    
    On Error Resume Next ' Continue if an error occurs (e.g., not a task item)
    
    ' Check if the current item is a task item
    If Application.ActiveInspector.CurrentItem.Class = olTask Then
        Set objTask = Application.ActiveInspector.CurrentItem
        
        ' Check if the task is overdue
        If objTask.DueDate < overdueDate And objTask.Complete = False Then
            ' Update custom field "TaskStatus" with "Overdue"
        Set UserProp = objTask.UserProperties.Find("TaskStatus")
        If UserProp Is Nothing Then
           Set objProp = objTask.UserProperties.Add("TaskStatus", olText, True)
        End If
        
            objTask.UserProperties("TaskStatus").Value = "Overdue"
            objTask.Save ' Save the changes
        End If
    End If
    
    Set objTask = Nothing ' Release the object reference
    On Error GoTo 0 ' Reset error handling
End Sub
 
Two typos. I forgot to change obj to objTask - not sure where the My came from, but removing that and it worked.

View attachment 4221

Try this code:
Code:
Sub UpdateTaskStatus()
    Dim objTask As Outlook.TaskItem
    Dim overdueDate As Date
    Dim objProp As Outlook.UserProperty
   
    ' Define the overdue threshold (e.g., overdue if due date is in the past)
    overdueDate = Date
   
    On Error Resume Next ' Continue if an error occurs (e.g., not a task item)
   
    ' Check if the current item is a task item
    If Application.ActiveInspector.CurrentItem.Class = olTask Then
        Set objTask = Application.ActiveInspector.CurrentItem
       
        ' Check if the task is overdue
        If objTask.DueDate < overdueDate And objTask.Complete = False Then
            ' Update custom field "TaskStatus" with "Overdue"
        Set UserProp = objTask.UserProperties.Find("TaskStatus")
        If UserProp Is Nothing Then
           Set objProp = objTask.UserProperties.Add("TaskStatus", olText, True)
        End If
       
            objTask.UserProperties("TaskStatus").Value = "Overdue"
            objTask.Save ' Save the changes
        End If
    End If
   
    Set objTask = Nothing ' Release the object reference
    On Error GoTo 0 ' Reset error handling
End Sub
Excellent! That works. Now to figure out the best way to automatically apply that tag to any overdue tasks.

Thanks again Diane
 
Excellent! That works. Now to figure out the best way to automatically apply that tag to any overdue tasks.

Thanks again Diane
If you want to have it applied like daily without your doing anything, you can use a reminder to trigger the macro. If you want it applied automatically when you open a task, you need to use NewInspector

This has an example of how to do it (but for email)


Basically.... this needs changed to taskitem and then the code do
Private Sub m_Inspector_Activate()
If TypeName(m_Inspector.currentItem) = "MailItem" then...

and then work the manual code into it.
 
Similar threads
Thread starter Title Forum Replies Date
S Task Update coming as email and not updating task Using Outlook 3
Diane Poremsky Task Request Status Update Address Missing Using Outlook 0
Bachelle Macro to Update Existing Task from New Email Outlook VBA and Custom Forms 3
2 Task auto update to shared group? Using Outlook 1
Q Sorting tasks by calculated field - automatically update task-fiel Outlook VBA and Custom Forms 9
P Can no longer sync Outlook with iPhone calendar after iPhone update to 17.1.1 Using Outlook 7
M "Attachment Detacher for Outlook" add in, does it update the server copy of the email? Using Outlook 1
icacream New password won't update Using Outlook 2
Commodore Safe way to add or update holidays; Windows Notifications issue Using Outlook 8
O Batch update calendar Using Outlook 3
R Outlook 365 update sets delete from server flag Using Outlook 1
V Update new custom field Outlook VBA and Custom Forms 5
P Outlook 2019 UI changes after 20H2 update Using Outlook 1
D Change Microsoft Account password - what to do to update on all devices Using Outlook 4
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
B Outlook 2016 Retail C2R keeps logging since update? Using Outlook 0
C Not sync folders not found after MS Outlook 365 update Using Outlook 1
W September 2020 - No Default Email Client message after Office Update Using Outlook 1
D.Moore VBA script fail after Office 365 update Using Outlook 8
W April 2020 Office 365 Update - Add-Ons fail after Office 365 Update Using Outlook 6
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
Jennifer Murphy Grant R/W (update) access to a friend Using Outlook 3
L Favorites don't update Using Outlook 1
F Copy and replace not update contact in another pst Using Outlook 0
M Message list font changed after update Using Outlook 2
M Quicken One Step Update Bill Reminders Not Syncing to Outlook Using Outlook 1
O Windows 1803 update : QAT and toolbar changed, language pack gone... Using Outlook 5
J Updating existing entry on shared calendar wants to send update from delegate Using Outlook 0
N Using email notification to update calendar events? Outlook VBA and Custom Forms 4
K Update Appointment category when changed in Excel Using Outlook 3
V Outlook 2003 problem with Windows 10 Creators Update 1709 Using Outlook 0
G Windows Update Causes BCM Database Access Problem? BCM (Business Contact Manager) 4
K Update subject based on text in body Outlook VBA and Custom Forms 3
A Creating Progress Bar or Status Bar Update Outlook VBA and Custom Forms 0
M Recent Update Did not Fix Search Problems Using Outlook 7
R Custom Contact Form how to update when loaded. Outlook VBA and Custom Forms 6
C Update Notes for Meeting Attendees Using Outlook 8
A Attendee Update Outlook Meeting Invite Using Outlook 0
Diane Poremsky Outlook Email Security Update Using Outlook 0
Diane Poremsky Remove Office 2013 Update Banner Using Outlook 0
B IMAP folders don't update when Outlook 365 opens Using Outlook 0
J Outlook Macro to Update Sharepoint Excel File Using Outlook 1
CWM330 Importing from Eudora Update Using Outlook 5
Cameron Piper Automatically update custom forms across multiple computers Outlook VBA and Custom Forms 1
M Update field codes when opening Outlook Template Outlook VBA and Custom Forms 2
I Microsoft Security Update KB3097877 Using Outlook 14
S Outlook 2010: October 2015 Update Using Outlook 0
S Outlook 2013: October 2015 Update Using Outlook 0
Diane Poremsky Update Contacts with a New Company Name and Email Address Using Outlook 0
Diane Poremsky Update Contacts with a New Company Name and Email Address Using Outlook 0

Similar threads

Back
Top