Projects as contacts code (thanks to whkratz, xoff)
There are two sections of code to copy and paste: This section, to copy and paste into ThisOutlookSession, and another section, to c & p into a new module (mine is named Utilities).
' Module : ThisOutlookSession
' Description:
' Procedures : Application_Startup()
' objTaskItems_ItemChange(ByVal pobjItem As Object)
' Modified :
' 11/13/03 WHK
' Modified 4/16/2005 EFG
' --------------------------------------------------
Private WithEvents objTaskItems As Items
Private Sub Application_Quit()
Set objTaskItems = Nothing
End Sub
Private Sub Application_Startup()
On Error GoTo PROC_ERR
TestAddComboBoxToCommandBar 'adds 3 comboboxes to a commandbar named Advanced
'comboboxes are Projects, Defer, and Context
Set objTaskItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks).Items
'yes, that's the taskitems folder
'all these various outlook variables
'give me a pain in the ass. I'm
'hardcoding the whole thing.
PROC_EXIT:
Exit Sub
PROC_ERR:
'removed errorlog
Resume PROC_EXIT
End Sub
Private Sub objTaskItems_ItemChange(ByVal pobjItem As Object)
On Error GoTo PROC_ERR
Dim objNewTask As TaskItem 'will be new N/A task
Dim intAns As Integer 'input from msgbox
Dim objProj As ContactItem 'found in folder "Projects"
Dim strSubject As String 'will be subject of next action task
Dim strProject As String 'name of project
Dim strBody As String 'body field of found project, may contain formatted list of NAs
Dim posCR As Integer 'carriagereturn position, delimits NAs in body field of found project
If pobjItem.Links.Count = 0 Then Exit Sub 'no links, no project, nothing to do
If pobjItem.Links.Count = 1 And InStr(1, pobjItem.Links.Item(1).Name, "[") = 1 Then
'project names must be delimited with brackets [ProjectName]
'limited now to only project contacts, routine will fail if there are other contacts which are not projects
'project contacts are linked
strSubject = pobjItem.Subject 'allows use in msgbox with completion announcement
strProject = pobjItem.Links.Item(1).Name
'open a project's body field with list of NAs
Set objProj = Application.GetNamespace("mapi").Folders.Item(1).Folders("Projects").Items.Item(strProject)
strBody = objProj.Body
If pobjItem.Status = 2 Then 'that would be completed
intAns = MsgBox("You have completed a Project-related Task." _
& vbCrLf & "Task: " & strSubject & vbCrLf & "Project: " _
& strProject & vbCrLf & "Do you want to create a new Next Action for the Project?", _
36, "Next Action?")
End If
If intAns = 6 Then 'Not testing for anything else
Set objNewTask = pobjItem.Copy 'why not? It's already linked to the project, and probably in the right context
If Left(strBody, 1) = "-" Then '- dash/space will signify
'the beginning of a next action in the project body field
If InStr(strBody, vbCrLf) Empty Then
posCR = InStr(strBody, vbCrLf)
strSubject = Mid(strBody, 3, (posCR - 3))
strBody = Right(strBody, Len(strBody) - posCR - 1)
strBody = strBody & vbCrLf & "x " & strSubject
objProj.Body = strBody 'takes first line of body field, uses it, moves it to end with x
objProj.Save
Else
posCR = Len(strBody) 'just a 1 liner, cut, use, and replace
strSubject = Mid(strBody, 3, (posCR - 2))
strBody = "x " & strSubject
objProj.Body = strBody 'takes first line of body field, uses it, moves it to end with x
objProj.Save
End If
Else
strSubject = InputBox("Taskname?") 'no NAs in body field
End If
objNewTask.Subject = strSubject
objNewTask.Status = olTaskNotStarted
objNewTask.Display
End If
End If
Set objProj = Nothing
Set objNewTask = Nothing
Set pobjItem = Nothing
PROC_EXIT:
Exit Sub
PROC_ERR:
Resume PROC_EXIT
End Sub