在outlook迭代事件中添加两次来自outlook

时间:2015-04-09 17:47:45

标签: office-interop outlook-addin

我需要从Outlook加载项item_Add事件中回复两次,一旦邮件到达就再回复一次,经过一些处理后再回复一次。注册回调的代码如下所示:

void items_ItemAdd(object Item)
{
  Outlook.MailItem mail = (Outlook.MailItem)Item;
  Outlook.MailItem reply = mail.Reply();
  // Add some stuff to reply body and subject
  reply.Send();
  Marshal.ReleaseComObject(reply); 
  // Some processing for few seconds
  reply = mail.Reply();
  // Update reply body and subject
  reply.Send()
  // Release reply object
}

我遇到的问题是在处理完成后最后发出2个回复。有没有办法立即发送第一个回复而不是等到结束?

2 个答案:

答案 0 :(得分:0)

注意,您从事件处理程序运行代码。处理程序应将控制流返回到Outlook以处理您的回复。

您可以尝试使用Namespace类的SyncObjects属性来强制同步。

Public Sub Sync() 
  Dim nsp As Outlook.NameSpace  
  Dim sycs As Outlook.SyncObjects  
  Dim syc As Outlook.SyncObject  
  Dim i As Integer  
  Dim strPrompt As Integer  
  Set nsp = Application.GetNamespace("MAPI")  
  Set sycs = nsp.SyncObjects  
  For i = 1 To sycs.Count  
    Set syc = sycs.Item(i)  
    strPrompt = MsgBox("Do you wish to synchronize " & syc.Name &"?", vbYesNo)  
    If strPrompt = vbYes Then  
     syc.Start   
    End If  
  Next  
End Sub

答案 1 :(得分:0)

我修改了以下代码:

...
outlookNameSpace = this.Application.GetNamespace("MAPI");
...
void items_ItemAdd(object Item)
{
  Outlook.MailItem mail = (Outlook.MailItem)Item;
  Outlook.MailItem reply = mail.Reply();
  // Add some stuff to reply body and subject
  reply.Send();
  outlookNameSpace.Session.SendAndReceive(false);
  Marshal.ReleaseComObject(reply); 
  // Some processing for few seconds
  reply = mail.Reply();
  // Update reply body and subject
  reply.Send()
  // Release reply object
}

由于