2013-02-05 91 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應用程序。 –