2017-09-18 57 views
0

我正在使用下面的两个代码之一下载Outlook邮件,另一个更新它。第一个下载邮件的代码没有问题。但是,当我使用第二个代码来查找任务并对相应的记录集进行一些添加时,有时错误的记录集正在更新可以帮助我吗? 的senttime和sentto是反映了错误的任务如何编辑匹配记录集?

首先代码:

Private Sub getml() 
Dim rst As DAO.Recordset 
Dim OlApp As Outlook.Application 

Dim inbox As Outlook.MAPIFolder 
Dim inboxItems As Outlook.Items 
Dim Mailobject As Object 
Dim db As DAO.Database 
Dim var As variant 
Set db = CurrentDb 

Set OlApp = CreateObject("Outlook.Application") 
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox) 
Set rst= CurrentDb.OpenRecordset("mls") 
Set inboxItems = inbox.Items 
On error resume next 
For Each Mailobject In inboxItems 
    set var = MailObject.UserProperties.Find("taskID") 
IF Not (var Is Nothing) Then 
     With rst 
      .FindFirst "task=" Chr(34) & var & Chr(34) 
     If .NoMatch then 
      .AddNew 
      !task= var.value & "" 
      .Update 

      Mailobject.UnRead = False 
     End If 
    End With 
End If 
Next 
Set OlApp = Nothing 
Set inbox = Nothing 
Set inboxItems = Nothing 
Set Mailobject = Nothing 
End sub 

二码

Private Sub stml() 
Dim rst As DAO.Recordset 
Dim OlApp As Outlook.Application 

Dim inbox As Outlook.MAPIFolder 
Dim inboxItems As Outlook.Items 
Dim Mailobject As Object 
Dim db As DAO.Database 
Dim var As variant 
Set db = CurrentDb 

Set OlApp = CreateObject("Outlook.Application") 
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderSentMail) 
Set rst= CurrentDb.OpenRecordset("mls") 
Set inboxItems = inbox.Items 
On error resume next 
For Each Mailobject In inboxItems 
    set var = MailObject.UserProperties.Find("taskID") 
IF Not (var Is Nothing) Then 
     With rst 
      .FindFirst "task=" Chr(34) & var & Chr(34) 
     If not .NoMatch then 
      .edit 
      !senttime= MailObject.Receivedtime 
      !sentto = mailobject.to 
      .Update 

      Mailobject.UnRead = False 
     End If 
    End With 
End If 
Next 
Set OlApp = Nothing 
Set inbox = Nothing 
Set inboxItems = Nothing 
Set Mailobject = Nothing 
End sub 

回答

1

你可以只添加第二个检查,以验证该记录是你想要的一个。

If Not (var Is Nothing) Then 
     With rst 
      .FindFirst "task=" Chr(34) & var & Chr(34) 
     If Not .NoMatch Then 
      If !task = CStr(Nz(var)) 
       .edit 
       !senttime= MailObject.Receivedtime 
       !sentto = mailobject.to 
       .Update 

       Mailobject.UnRead = False 
      End If 
     End If 
    End With 
End If 

注意,这可能是有用的打印或登录失败的!task = CStr(Nz(var))检查记录。我最好的猜测是var对于这些值是Null