Drag/Drop from AddIn into Excel

D

David Thielen

Hi;

In our Word AddIn we support dragging data from our AddIn and dropping it
into Word. This actually ended up being pretty easy (after a bunch of pain
trying other ways) where we just created an rtf document in the clipboard
text that was a complete Word table. When it was dropped into Word it was as
though a table was selected in another Word document and dragged/dropped into
Word.

Is there a way to do the same thing in Excel? I am going to try doing it
with CSV but is there anything I should watch out for to make sure that if I
create a CSV table of 5 columns by 3 rows that it will drop it in across the
existing cells?

And is CSV the best way to do this?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
J

Jialiang Ge [MSFT]

Hello Dave,

My suggestion is not to use CSV data format, because
1. A csv file cannot be loaded into a rich text box windows control, and it
cannot give a direct picture of the table to the users.
2. A csv file cannot be dragged/dropped by simply calling
DataObject obj = new DataObject();
Obj.SetData(DataFormats.CommaSeparatedValue, true, "1,2,3,4");
richTextBox1.DoDragDrop(obj, DrapDropEffects.Copy);

because if a dataobject's data format is CommaSeparatedValue, it requires
the target form to call 'GetData(DataFormats.CommaSeparatedValue)',
otherwise, the paste result will be some garbage characters.

My suggestion is to simply use a txt file and DataFormats.Text.
The txt file contains values separated with tab '\t'. For instance,
A B C D
E F G H
because
1. A txt file can be easily loaded into a rich text box control, and be
shown like a real table
2. The drag and drop operation is also very simple. See the following
example code:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
this.richTextBox1.Text = "1\t2\t3\t4"; // or load the content from a txt
file.
DataObject mydataobj = new DataObject();
mydataobj.SetData(DataFormats.Text, false, this.richTextBox1.Text);
this.richTextBox1.DoDragDrop(mydataobj, DragDropEffects.Copy);
}

The code above worked well in my side. Please have a try and let me know if
can help you resolve the problem.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top