2015-04-02 69 views
2

我正在創建一個WPF應用程序。我想從Outlook中將電子郵件拖到這個wpf應用程序中,應用程序需要將它保存在特定的文件夾中。我曾嘗試使用http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C文章。它適用於winform。我在修復所有編譯時錯誤後嘗試在WPF中使用相同的代碼,但仍然無法正常工作。如何將Outlook電子郵件拖放到WPF應用程序中

我已經在網上搜索了很多,但找不到任何針對此問題的工作解決方案。有人可以提供任何工作示例代碼嗎?

+0

outloock你有什麼版本? – 2015-04-02 05:46:40

+0

我正在使用outlook 2013 – yrahman 2015-04-02 06:03:34

+0

奇怪......我正在分析這封信。嘗試分析下面的EmailObject代碼。在訪問格式爲'FileContent'的數據時訪問 – 2015-04-02 06:07:26

回答

1

!!!添加引用:「Microsoft.Office.Interop.Outlook.dll」!!! (在你的硬盤搜索)

Analyzee您DragObject:

WPF:

<Window x:Class="WpfApplication1.MainWindow" 
     x:Name="thisForm" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <TextBlock TextWrapping="WrapWithOverflow" Drop="ContainerDrop" DragOver="ContainerDragOver" Name="f_DropText" AllowDrop="True"/> 
</Window> 

C#

using System; 
using System.IO; 
using System.Text; 
using System.Windows; 
namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
    private void ContainerDrop(object sender, DragEventArgs e) 
    { 
     f_DropText.Text = ""; 
     StringBuilder sb = new StringBuilder(); 
     foreach (string format in e.Data.GetFormats()) 
     { 
     sb.AppendLine("Format:" + format); 
     try 
     { 
      object data = e.Data.GetData(format); 
      sb.AppendLine("Type:" + (data == null ? "[null]" : data.GetType().ToString())); 
      if (format == "FileGroupDescriptor") 
      { 
      Microsoft.Office.Interop.Outlook.Application OL = new Microsoft.Office.Interop.Outlook.Application(); 
      sb.AppendLine("##################################################"); 
      for (int i = 1; i <= OL.ActiveExplorer().Selection.Count; i++) 
      { 
       Object temp = OL.ActiveExplorer().Selection[i]; 
       if (temp is Microsoft.Office.Interop.Outlook.MailItem) 
       { 
       Microsoft.Office.Interop.Outlook.MailItem mailitem = (temp as Microsoft.Office.Interop.Outlook.MailItem); 
       int n=1; 
       sb.AppendLine("Mail " + i + ": " + mailitem.Subject); 
       foreach (Microsoft.Office.Interop.Outlook.Attachment attach in mailitem.Attachments) 
       { 
        sb.AppendLine("File " + i + "."+n+": " + attach.FileName); 
        sb.AppendLine("Size " + i + "."+n+": " + attach.Size); 
        sb.AppendLine("For save using attach.SaveAsFile"); 
        ++n; 
       } 
       } 
      } 
      sb.AppendLine("##################################################"); 
      } 
      else 
      sb.AppendLine("Data:" + data.ToString()); 
     } 
     catch (Exception ex) 
     { 
      sb.AppendLine("!!CRASH!! " + ex.Message); 
     } 
     sb.AppendLine("====================================================="); 
     } 
     f_DropText.Text = sb.ToString(); 
    } 
    private void ContainerDragOver(object sender, DragEventArgs e) 
    { 
     e.Effects = DragDropEffects.Copy; 
     e.Handled = true; 
    } 
    } 
} 
+0

。我得到這個錯誤'無效lindex(異常從HRESULT:0x80040068(DV_E_LINDEX))' – yrahman 2015-04-02 06:16:20

+0

同時訪問格式'FileName'和'FileNameW'的數據時,我得到這個錯誤'對象引用未設置爲對象的實例。' – yrahman 2015-04-02 06:18:02

+0

用OutLook interlop對象再次重複! – 2015-04-02 06:55:17

1

而不是試圖恢復需要檢測丟棄的郵寄對象某些東西是從Outlook中獲取的,然後使用System.Runtime.InteropServices.Marshal.GetActiveObject()方法連接到正在運行的Outlook實例,該方法獲取正在運行的實例e從運行對象表(ROT)中指定的對象。

然後,您可以使用Explorer類的Selection屬性獲取Selection對象。它返回一個Selection對象,該對象包含在資源管理器窗口中選擇的一個或多個項目。

-1
private void lbAttachments_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) 
     { 
      e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor") ? DragDropEffects.All : DragDropEffects.None; 
     } 

     private void lbAttachments_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
     { 
      if (e.Data.GetDataPresent("FileGroupDescriptor")) 
      { 
       try { 
        var dataObject = new OutlookDataObject(e.Data); 
        var filePaths = new StringCollection(); 

        string[] fileContentNames = (string[])dataObject.GetData("FileGroupDescriptor"); 
        if (fileContentNames.Count() > 0) 
        { 
         var fileStreams = (MemoryStream[])dataObject.GetData("FileContents"); 

         for (int fileIndex = 0; fileIndex < fileContentNames.Length; fileIndex++) 
         { 
          var ms = fileStreams[fileIndex]; 
          var bytes = ms.ToArray(); 

          AddAttachment(fileContentNames[fileIndex], bytes); 
         } 
        } 
       } 
       catch(Exception) { /* ignore */ } 

      } 
      else if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
      { 
       string[] s = (string[])e.Data.GetData(DataFormats.FileDrop); 
       if (s == null) 
        return; 

       foreach (string file in s) 
        AddAttachment(file); 
      } 
private void AddAttachment(string argFilename) 
     { 
      var daten = File.ReadAllBytes(argFilename); 
      AddAttachment(argFilename, daten); 
     } 
0

最後,我得到了這個工作。

我把這個傢伙的代碼在http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx並改變它與WPF一起使用。

但是這並不容易,因爲在System.Windows的基礎COM結構中幾乎沒有變化的構造,所以它不能僅僅通過從system.windows更改爲s.w.forms.IDataObject而工作。

然後我發現這個github上的代碼從這個線程
https://social.msdn.microsoft.com/Forums/vstudio/en-US/5853bfc1-61ac-4c20-b36c-7ac500e4e2ed/how-to-drag-and-drop-email-msg-from-outlook-to-wpfor-activex-for-upload-and-send-them-out-via?forum=wpf

http://gist.github.com/521547 - 這是新的IDataObject與WPF工作。

有修正,以適應System.Windows.IDataObject而不是S.W.Forms.IDataObject和「_innerData」應該被用於查詢數據,以修復一些內存訪問問題一起。那傢伙釘了它!

相關問題