update outlook task from acess with vba

2019-09-01 18:52发布

问题:

I am trying to update outlook tasks from Access using VBA. See the code below. This is a procedure within a class. I first look for the task matching two criteria (this is working) and then I would like to update the task fields.

Would someone know why this is not working and if there is a way to do it?

The code is running without any error message showing up, but the task does not get updated. I would prefer not to delete the task and create a similar one (I would lose the creation date).

Many Thanks

Sub updateOutLooktask()

Dim objItems As outlook.Items
Dim ol As New outlook.Application
Dim olns As outlook.NameSpace
Dim cf As outlook.MAPIFolder
Dim myRecipient As outlook.Recipient
Dim myNamespace As outlook.NameSpace
Dim var As Collection
Dim i As Integer
Dim iTasks As Integer




Set myRecipient = myNamespace.CreateRecipient(Me.strRecipient)
myRecipient.Resolve

Set myNamespace = ol.GetNamespace("MAPI")
Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetSharedDefaultFolder(myRecipient, olFolderTasks)
Set objItems = cf.Items

imax = objItems.Count
i = 1

Do While i <= imax

    If objItems(i).ConversationID = Me.strConversationID And objItems(i).EntryID = Me.strEntryID Then
        objItems(i).Subject = Me.strSubject
        objItems(i).Body = Me.strBody
        objItems(i).Importance = Me.intImportance
        objItems(i).Owner = Me.strOwner
        objItems(i).StartDate = Nz(Me.dtStartDate, #1/1/4501#)
        objItems(i).DueDate = Nz(Me.dtDueDate, #1/1/4501#)
        objItems(i).Status = Me.intStatus
        objItems(i).PercentComplete = Me.intPercentComplete
        objItems(i).Complete = Me.blComplete
        objItems(i).TotalWork = Me.intTotalWork
        objItems(i).ActualWork = Me.intActualwork
        objItems(i).Categories = Me.strCategories
        objItems(i).Save

        Exit Do
    End If
    i = i + 1
Loop

End Sub

回答1:

Thanks For you help, it worked using your code. I don't really understand why but i am only a beginner so hopefully it will make sense soon.

Also I have used Application.Session.GetItemFromID(objItem) as you mentioned and it really simplify the procedure:

Sub updateOutLooktask()

Dim ol As New outlook.Application

Set objitem = ol.Session.GetItemFromID(Me.strEntryID)

objitem.Subject = Me.strSubject
objitem.Body = Me.strBody
objitem.Importance = Me.intImportance
objitem.Owner = Me.strOwner
objitem.StartDate = Nz(Me.dtStartDate, #1/1/4501#)
objitem.DueDate = Nz(Me.dtDueDate, #1/1/4501#)
objitem.Status = Me.intStatus
objitem.PercentComplete = Me.intPercentComplete
objitem.Complete = Me.blComplete
objitem.TotalWork = Me.intTotalWork
objitem.ActualWork = Me.intActualwork
objitem.Categories = Me.strCategories
objitem.Save


End Sub

Thanks again for your help



回答2:

The line objItems(i).Save translates into objItems.Item(i).Save. Each "." gives you a brand new COM object, and you end up calling Save on an object different from the one you were modifying. Also, why are you using a while loop instead of for?

for i = 1 to objItems.Count 
  set objItem = objItems(i)
    If objItem.ConversationID = Me.strConversationID And objItem.EntryID = Me.strEntryID Then
        objItem.Subject = Me.strSubject
        objItem.Body = Me.strBody
        objItem.Importance = Me.intImportance
        objItem.Owner = Me.strOwner
        objItem.StartDate = Nz(Me.dtStartDate, #1/1/4501#)
        objItem.DueDate = Nz(Me.dtDueDate, #1/1/4501#)
        objItem.Status = Me.intStatus
        objItem.PercentComplete = Me.intPercentComplete
        objItem.Complete = Me.blComplete
        objItem.TotalWork = Me.intTotalWork
        objItem.ActualWork = Me.intActualwork
        objItem.Categories = Me.strCategories
        objItem.Save

        Exit for
    End If
next

Secondly, there is absolutely no point looping over all items in a folder if you already know the entry id - just use Application.Session.GetItemFromID(Me.strEntryID)



标签: vba outlook task