2013-02-05 90 views
1

我要去访问Outlook MAPI文件夹和接收邮件访问邮件对象MAPI address.Here是我的方法无法从调用函数

public static string GetSenderEmailAddress(Microsoft.Office.Interop.Outlook.MailItem mapiObject) 
    { 
     Microsoft.Office.Interop.Outlook.PropertyAccessor oPA; 
     string propName = "http://schemas.microsoft.com/mapi/proptag/0x0065001F"; 
     oPA = mapiObject.PropertyAccessor; 
     string email = oPA.GetProperty(propName).ToString(); 
     return email; 
    } 

当按钮的单击事件称为,我需要的大火,方法和检索邮件地址。

按钮点击事件在这里。

 private void button3_Click(object sender, RibbonControlEventArgs e) 
     { 

string mailadd = ThisAddIn.GetSenderEmailAddress(Microsoft.Office.Interop.Outlook.MailItem); 
     System.Windows.Forms.MessageBox.Show(mailadd); 

} 

错误放在这里

Microsoft.Office.Interop.Outlook.MailItem是一个“type'which无效在给定上下文

这是我的第一个插件,有谁知道如何实现这个结果?

+0

你确定的MailItem对象是有效的?它从何而来? –

+0

当我调用按钮单击事件错误happend.It函数来自调用函数 –

+0

好吧,调用者函数从哪里得到它? –

回答

0

您可以使用RibbonControlEventArgs访问Context,它将为您提供MailItem实例。

private Outlook.MailItem GetMailItem(RibbonControlEventArgs e) 
{ 
    // Inspector Window 
    if (e.Control.Context is Outlook.Inspector) 
    { 
     Outlook.Inspector inspector = e.Control.Context as Outlook.Inspector; 
     if (inspector == null) return null; 
     if (inspector.CurrentItem is Outlook.MailItem) 
      return inspector.CurrentItem as Outlook.MailItem; 
    } 
    // Explorer Window 
    if (e.Control.Context is Outlook.Explorer) 
    { 
     Outlook.Explorer explorer = e.Control.Context as Outlook.Explorer; 
     if (explorer == null) return null; 
     Outlook.Selection selectedItems = explorer.Selection; 
     if (selectedItems.Count != 1) return null; 
     if (selectedItems[1] is Outlook.MailItem) 
      return selectedItems[1] as Outlook.MailItem; 
    }  
    return null; 
} 

您可以添加此方法,然后利用它是这样的...

string mailAddress = string.Empty; 
Outlook.MailItem mailItem = GetMailItem(e); 
if (mailItem != null) 
    mailAddress = ThisAddIn.GetSenderEmailAddress(mailItem); 
+0

这是假定Outlook功能区上的按钮。它看起来像OP有一个Win Forms应用程序。 –