Access.2007.VBA.Bibl..

(John Hannent) #1

For a more realistic scenario, in which you want to create tasks based on data in an Outlook table,
see the CreateProjectTasksfunction (it can be run from the mcrCreateProjectTasks macro).
This function creates an Outlook task for each record in tblContactsWithProjects that has not had
its supplies replenished for a month or more, and writes data from several fields in the Access
record to the task item:


Public Function CreateProjectTasks()

On Error GoTo ErrorHandler

Dim fldTasks As Outlook.Folder
Dim tsk As Outlook.TaskItem
Dim dteMonthAgo As Date
Dim dteReplenished As Date
Dim dteNextMonday As Date
Dim strProject As String

Set appOutlook = GetObject(, “Outlook.Application”)
Set nms = appOutlook.GetNamespace(“MAPI”)
Set fldTasks = nms.GetDefaultFolder(olFolderTasks)
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(“tblContactsWithProjects”)
dteMonthAgo = DateAdd(“m”, -1, Date)
dteNextMonday = NextMonday()

With rst
Do While Not .EOF

Check whether supplies were last replenished more than a month ago.


dteReplenished = Nz(![SuppliesReplenished])
strProject = Nz(![CurrentProject])

If dteReplenished < dteMonthAgo Then

Create a new task in the local Tasks folder.


Set tsk = fldTasks.Items.Add
tsk.Subject = “Replenish supplies for “ _
& strProject
tsk.StartDate = dteNextMonday
tsk.Status = 0
tsk.Importance = 1
tsk.Close (olSave)
End If
.MoveNext
Loop
End With

MsgBox “Outlook project tasks created”

Working with Outlook Items 8

Free download pdf