2011-10-18 79 views
8

我正在使用WPF,我正在嘗試使拖放文本框變爲拖動狀態。
在這個文本框中,我想獲得從Outlook拖動的電子郵件的正文。
該代碼有效,但我認爲我需要一些「重置」ActiveExplorer的原因,現在它只顯示最後一個「新」電子郵件,我拖入文本框。從Outlook電子郵件獲取正文[Drag'n'Drop]

實施例:

將電子郵件1 - >文本框 - 顯示電子郵件1

將電子郵件2 - >文本框 - 顯示電子郵件2

將電子郵件1 - >文本框 - 顯示電子郵件2和電子郵件1將不會顯示,因爲它已經存在於ActiveExplorer中,它將顯示電子郵件2.


希望我的問題對你有點清楚..
在此先感謝!

XAML代碼:

<TextBox 
    Name="myTextbox" 
    AllowDrop="True" 
    PreviewDragEnter="email_DragEnter" 
    PreviewDrop="email_Drop" /> 

XAML代碼隱藏:

private void email_DragEnter(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 

    private void email_Drop(object sender, DragEventArgs e) 
    { 
     Outlook.ApplicationClass oApp = new Outlook.ApplicationClass(); 
     Outlook.Explorer oExplorer = oApp.ActiveExplorer(); 
     Outlook.Selection oSelection = oExplorer.Selection; 

     foreach (object item in oSelection) 
     { 
      Outlook.MailItem mi = (Outlook.MailItem)item; 
      myTextbox.Text = mi.Body.ToString(); 
     } 
    } 

回答

9

我將下面的DragDrop事件的oApp聲明移出,並且它按預期工作。

void Startup() 
{ 
    _Outlook = new Outlook.Application(); 
} 

Outlook.Application _Outlook = null; 

private void Form1_DragEnter(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Copy; 
} 

private void Form1_DragDrop(object sender, DragEventArgs e) 
{ 
    richTextBox1.Text = ""; 
    Outlook.Explorer oExplorer = _Outlook.ActiveExplorer(); 
    Outlook.Selection oSelection = oExplorer.Selection; 

    foreach (object item in oSelection) 
    { 
     Outlook.MailItem mi = (Outlook.MailItem)item; 
     richTextBox1.AppendText(mi.Body.ToString() + "\n----------------------------------------\n"); 
    } 
} 

-------- --------編輯

或者是有可能的是你顯示,因爲這個循環的唯一的最後一個項目?

foreach (object item in oSelection) 
{ 
    Outlook.MailItem mi = (Outlook.MailItem)item; 
    myTextbox.Text = mi.Body.ToString(); //<--- Only last items text 
} 
+0

這工作正常,但是如何獲得1封電子郵件?那麼只有最後一個郵件被拖動了? – jefsmi

+0

對不起,我不知道我的理解正確,但如果我拖動一個項目,我只看到它的文本。如果我選擇多個文本,它們的所有文本都會顯示爲richtextbox1 –

+0

並按以下順序拖動:郵件1 - >清除文本框後拖動其他郵件 - >清除文本框之後再拖動郵件1,然後再顯示第二個拖動郵件而不是郵件1 – jefsmi

0

使用14.0.0.0版本的Microsoft.Office.Interop.Outlook.dll的我不能使用Outlook.ApplicationClass對象。

取而代之,我在您給出的示例中使用了Outlook.Application,它的工作原理類似於魅力(使用Windows 7 & Outlook 2007 SP2進行測試)。我可以隨意拖動&拖放電子郵件。


PS:MSDN ExtractApplicationClass類:

「該類支持.NET Framework基礎結構,不適合直接在代碼中使用」

+1

我知道Outlook.ApplicationClass嵌入在版本14.0.0.0,但這不是我的問題。我可以拖放郵件,但是如果我放下多封郵件,它們會保存在Outlook.Explorer中(「就像是一種列表」),我只想顯示郵件並將其拖入文本框。目前,該foreach從Outlook.Explorer列出了一個列表,並顯示了最後一次丟棄的郵件,我只希望顯示我的活動郵件。 – jefsmi

+0

你可以請你精確的你的情況?在第一個示例中,您拖動的是單個電子郵件,而在您的上一條評論中,您似乎指的是拖動多個電子郵件。 –

+0

我正在拖動signle電子郵件。我的screnario:[#1]。在textbox中拖動email_1 = OK ==>我清除文本框[#2]。在textbox中拖動email_2 = OK ==>我清除文本框[#3]。將email_1拖動到textbox = NOT OK(它顯示email_2)。這是因爲email_2是ActiveExplorer中的最後一封郵件,並且將成爲我的foreach循環中的最後一個mailItem。 – jefsmi

1

我更新了L.B的回答。他的DragEnter EventHandler自動假定用戶從Outlook中刪除了某些東西。

結果是,如果用戶放棄了其他內容(文件,選定文本,...),代碼仍然會查看Outlook中當前選定的電子郵件,並忽略實際放置的內容。

代碼:

Private _Outlook As Outlook.Application = Nothing 

Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 
    _Outlook = New Outlook.Application() 
End Sub 

Private Sub Form_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter 
    Dim outlookRequiredFormats = New String() { _ 
     "RenPrivateSourceFolder", _ 
     "RenPrivateMessages", _ 
     "RenPrivateItem", _ 
     "FileGroupDescriptor", _ 
     "FileGroupDescriptorW", _ 
     "FileContents", _ 
     "Object Descriptor"} 

    If outlookRequiredFormats.All(Function(requiredFormat) e.Data.GetDataPresent(requiredFormat)) Then 
     e.Effect = DragDropEffects.Copy 
    Else 
     e.Effect = DragDropEffects.None 
    End If 
End Sub 

Private Sub Form_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop 
    Dim oExplorer As Outlook.Explorer = _Outlook.ActiveExplorer() 
    Dim oSelection As Outlook.Selection = oExplorer.Selection 
    Dim i As Integer = 0 
    For Each item As Object In oSelection 
     Dim mi As Outlook.MailItem = DirectCast(item, Outlook.MailItem) 
     mi.SaveAs("C:\YourPath\message" & i & ".msg") 
     i += 1 
    Next 

有選擇的Outlook項目到Outlook.MailItem直接投。該代碼因此只適用於實際的電子郵件。這也有可能處理Outlook.MeetingItem,Outlook.ContactItem,Outlook.NoteItem和可能更多。

相關問題