Click here to Skip to main content
15,879,326 members
Articles / Productivity Apps and Services / Microsoft Office

Drag and drop Outlook attachments

Rate me:
Please Sign up or sign in to vote.
3.31/5 (11 votes)
19 Jun 2008CPOL2 min read 54.2K   1.2K   20   11
This article demostrates how to do drag and drop Outlook attachments on a tree view node and describes the private Clipboard format of Outlook.

Introduction

Would like to drag n drop an Outlook attachment to a tree view and got stuck? Don't worry, I am here to help you.

When I came across this requirement, I searched the web and I found something which helped me a lot in developing my project, albeit with modifications. You can see the original code here: http://www.codeguru.com/cpp/i-n/internet/email/article.php/c3381/.

This link is pretty old and provides sample code, but for VB, not for MFC and using the VC6 compiler. One of the things which That I did not like was that it used COM. Here, I am using the MFC advanced OLE drag and drop mechanism which wraps the COM and provides a very simple mechanism for development.

The Outlook Clipboard format is not a standard one. It registers its private formats called CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTORA. Also remember that it does not register a Unicode version CFSTR_FILEDESCRIPTORW, and hence, even though my sample code will build on non-Unicode as well as Unicode, in the Unicode build, some Unicode characters may get lost when in use.

The first thing you start with is implementing the OnCreate() function of the treeview class and registering the window and the private clipboard format.

C++
 int CDragnDropView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CTreeView::OnCreate(lpCreateStruct) == -1)
        return -1;

    //Register the private clipboard formats of Outlook
    cp_format_contents = ::RegisterClipboardFormat(CFSTR_FILECONTENTS);

    //We need to use Non unicode version of CFSTR_FILEDESCRIPTOR even in unicode build
    cp_format_descriptor = ::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA);
    m_oleDropTarget.Register (this);
    //Enable drag and drop for Outlook attachments

    return 0;

}

The next step is to override the OnDragEnter(), OndragOver(), OnDrop(), and OnDragLeave() functions.

In the OnDragEnter() and OnDragOver() functions, you just filter out when to enable the drag and drop and when to disable it. The core functionality is put inside the OnDrop() function. If you start a drag and drop operation, but would not drop it on to the tree view or just presses Escape, the OnDragLeave() function will be called instead of OnDrop(), and this is the place where you should write the cleanup code, e.g., memory deletion etc.

In my sample project, I have a root node called Javed which points to nowhere, but it has two child nodes, Node1 and Node2, which point to D:\Javed\Node1 and D:\Javed\Node2, respectively. When you drop an Outlook attachment on to one of these nodes, it will be dropped to the respective node. You need to have these folders in your system; otherwise you need to change the path of the node in the OnDrop() function.

C++
void CDragnDropView::OnDrop()>
{
    //Other codes

    if (bStatus)
    {
        // Dump stream to a file 
        CString strFile_name,strLocation;
        CString strTargetNode = GetTreeCtrl().GetItemText(m_htTargetNode);
        if(!strTargetNode.CompareNoCase(_T("Node1")))
            strLocation = _T("D:\\Javed\\Node1\\");
        if(!strTargetNode.CompareNoCase(_T("Node2"))) 
            strLocation = _T("D:\\Javed\\Node2\\");
        strFile_name = file_descriptor.cFileName;
        strFile_name = strLocation + strFile_name;
        hr = StreamToFile(storage.pstm, strFile_name);
    }
}

Now you can download the sample project and get a deeper look into the code.

Points of Interest

Any feedback on this is appreciated.

License

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


Written By
Software Developer (Senior)
India India
Javed is software developer (Lead). He has been working on desktop software using C++\C# since 2005.

Comments and Discussions

 
QuestionTransferring a virtual file (Outlook message items) fails in CDragnDropView::OnDrop at GetData() Everytime Pin
GeorgeTholl22-Dec-20 4:27
GeorgeTholl22-Dec-20 4:27 
GeneralMy vote of 5 Pin
Victor Nijegorodov24-Nov-20 6:33
Victor Nijegorodov24-Nov-20 6:33 
BugFilename is substringed Pin
Aleksandar Dragosavac19-Jan-14 20:58
Aleksandar Dragosavac19-Jan-14 20:58 
GeneralSpecial Thanks Pin
Ali Rafiee7-Mar-10 9:36
Ali Rafiee7-Mar-10 9:36 
QuestionHow to implement Drag and drop for mail item Pin
MKC0022-Jul-09 22:45
MKC0022-Jul-09 22:45 
AnswerRe: How to implement Drag and drop for mail item Pin
Javed Akhtar Ansari3-Jul-09 0:33
Javed Akhtar Ansari3-Jul-09 0:33 
GeneralDownload Size Pin
ReorX3-Jan-08 1:03
ReorX3-Jan-08 1:03 
What the hell is in your downloads that requires 18 MB????
Which of the downloads is a sample, which one contains the library / interface?


The Saviour of the World is a Penguin and Linus Torvalds is his Prophet.

General[Message Deleted] Pin
Jiteman7-Jan-08 11:29
Jiteman7-Jan-08 11:29 
General[Message Deleted] Pin
Jiteman7-Jan-08 11:35
Jiteman7-Jan-08 11:35 
GeneralRe: Download Size Pin
Jiteman7-Jan-08 11:50
Jiteman7-Jan-08 11:50 
AnswerRe: Download Size Pin
Javed Akhtar Ansari19-Jun-08 20:58
Javed Akhtar Ansari19-Jun-08 20:58 

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

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