WPF Drag & Drop to External Application (Outlook)

0

I am attempting to implement some additional Drag & Drop functionality to my WPF application. I have Drag & Drop working when the source and target are within my application, but I need to be able to drag data from my application to Outlook (Desktop App) and have it be uploaded as a file upon the drop.

I am familiar with the standard DragDrop.DoDragDrop() method, and the events associated with typical drag-drops.

Current Markup:

MouseLeftButtonDown="Data_MouseLeftButtonDown"

Current Event Handler Code:

/// Getting the data that we want to export (For example only)
private void Data_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Write data to a file (For example only)
    var fileToExport = File.ReadAllLines("C:\\test.txt");

    if (e.OriginalSource is Grid grid && grid.DataContext is CustomDataObject cdo)
    {
        DragDrop.DoDragDrop(ControlGoesHere, fileToExport, DragDropEffects.Copy);
    }
}

I have tried most, if not all, of the other SO solutions I researched, but I am still unable to drop this data into Outlook. Additionally, I disabled "Just My Code" and enabled all CLR exceptions and saw this exception being thrown:

System.Runtime.InteropServices.COMException: 'Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))'

It is my understanding that DoDragDrop constructs a DataObject based on the data you pass in, and that you can instantiate the DataObject manually before passing it in for more control, etc, but I haven't gotten any configuration working so far.

There are a few things I'm uncertain of

  • Does the data that gets 'exported' need to come from the source control? (I successfully managed to export a string "Test" to Outlook that wasn't from the control.
  • In order to have DoDragDrop return something, I've got to press ctrl or other keys, is this due to the setup of my current implemenation not handling the drop logic properly (by using QueryContinueDrag for example?

WPF, .NET Framework 4.6.1

I can give more information as needed on request, just let me know. Thanks in advance!

c#
wpf
drag-and-drop
export
asked on Stack Overflow Mar 5, 2020 by ClicheCoffeeMug • edited Mar 5, 2020 by ClicheCoffeeMug

1 Answer

0

Outlook needs to know what it's receiving. To do that, you create a DataObject and give it a format type of FileDrop. FileDrop expects a string[] as the object value, so use a string[] to input your file PATH or paths if you have more than one. do NOT try and read the whole file.

string[] files = new string[1];
files[0] = ""; //fill this in with the file PATH
DataObject data = new DataObject(DataFormats.FileDrop, files);
DragDrop.DoDragDrop(listTest, data, DragDropEffects.Copy);

With this I was able to drag an item into the message area of an outlook e-mail and have it attach the file with the path I input.

answered on Stack Overflow Mar 5, 2020 by oppassum

User contributions licensed under CC BY-SA 3.0