我正在尝试使用VBA从Access更新Outlook任务。请参阅下面的代码。这是一个类中的过程。 我首先查找匹配两个条件的任务(这是有效的),然后我想更新任务字段。
有人知道为什么这不起作用以及是否有办法做到这一点?
代码运行时没有显示任何错误消息,但任务未更新。我宁愿不删除任务并创建一个类似的任务(我将失去创建日期)。
非常感谢
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
答案 0 :(得分:0)
objItems(i).Save
行转换为objItems.Item(i).Save
。每个“。”为您提供了一个全新的COM对象,并最终在与您正在修改的对象不同的对象上调用Save。另外,为什么使用while
循环代替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
其次,如果您已经知道条目ID,则绝对没有必要循环文件夹中的所有项目 - 只需使用Application.Session.GetItemFromID(Me.strEntryID)
答案 1 :(得分:0)
谢谢你的帮助,它使用了你的代码。我不明白为什么,但我只是一个初学者,所以希望它很快就会有意义。
我也提到了你提到的Application.Session.GetItemFromID(objItem)
,它确实简化了程序:
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
再次感谢您的帮助