Click here to Skip to main content
Click here to Skip to main content

Drag and Drop from Custom Task Pane to Microsoft Office Documents

, 31 Jan 2014
Rate this:
Please Sign up or sign in to vote.
This tip briefly explains drag and drop text from custom task pane to Excel\Word\Powerpoint

Introduction

Have you ever needed a text to be dragged and dropped into Office documents from your custom task pane? If yes, go ahead.

Background

In this tip, I refer to Excel, Word and Powerpoint 2010 as Office documents. Basically, Office documents allow drag and drop (D&D) content within the applications, across the Office applications and from other text editors like Wordpad, Microsoft Visual Studio, etc.

My previous article ResizeCustomTaskPane states the prerequisites that user should know before implementing this tip. Let us see simple D&D text from custom task pane.

Using the Code

Architecture and source code are the same as my previous article and to save time, I have explained only the heart of the code. Refer to the following image of this sample.

The taskpane has 4 buttons and all the button's mousedown events are hooked to a single function.

private void button1_MouseDown(object sender, MouseEventArgs e)    {
            if (MouseButtons == MouseButtons.Left)
            {
                string strValue = "I am " + (sender as Button).Text;
                if (OfficeType == OfficeAPPType.EXCEL || OfficeType == OfficeAPPType.WORD)
                                {
                                    (sender as Button).DoDragDrop(strValue, DragDropEffects.All);
                                }
                                else if (OfficeType == OfficeAPPType.PPT)
                                {
                                    if (m_rich == null)
                                        m_rich = new RichTextBox();
                    m_rich.Text = strValue;
                    IDataObject data = new DataObject();
                                        data.SetData(DataFormats.Rtf, m_rich.Rtf);
                    (sender as Button).DoDragDrop(data, DragDropEffects.All);                    
                                    }                 
                                }
                            } 

D&D for Powerpoint is little trickier than Excel and Word. Both Excel and Word support DataFormats.Text, so we can directly supply string value to DoDragDrop function. Powerpoint needs DataFormats.Rtf format, so supply IDataObject with RTF text. Here I used RichTextBox to get the RTF text. That's all we need to drag and drop the text from taskpane. Ideas better than this are always welcome. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sivaji1565
Software Developer
India India
No Biography provided

Comments and Discussions

 
BugUnable to test: broken licenses.licx PinmemberGustavo Miller29-Apr-14 2:43 
GeneralRe: Unable to test: broken licenses.licx PinmemberSivaji156529-Apr-14 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 31 Jan 2014
Article Copyright 2014 by Sivaji1565
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid