Link to home
Start Free TrialLog in
Avatar of JohnBPrice
JohnBPrice

asked on

Now 500 points! Drag and Drop Outlook mail messages into VB 6.0 app

I need to support dragging mail messages from outlook 2002 into my VB 6 app (whereupon I will tie them to a customer record & store them in the database.  I can get the OLEDragDrop event with a DataObject fine, but I don't know how to use the DataObject to get to the outlook.mailitem.  The DataObject i get supports formats 1, 13, -16370, and -16193.  Format 1 is text, and the text contains this

From    Subject Received    Size    
John Doe  Great News for Friends of Animals    Tue 5/18/2004 2:51 PM   8 KB    

which is clearly the columns I have shown in my outlook inbox.  Format 13 looks the same.  Formats -16370 and -16193 contain some binary info and look promising, but I haven't been able to find anything about them.

Does anyone know how to decode these formats and get to the outlook.mailitem from the OLE dropped DataObject?

I'll post the extra pts in a separate question...

ASKER CERTIFIED SOLUTION
Avatar of jkozee
jkozee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnBPrice
JohnBPrice

ASKER

Thanks jkozee, that will get me there.  Ugly as all get out, but it will get me there.

Alas, as you can see above and in my other question, I've been instructed that I am not allowed to give 1000 pts. by posting two questions, you get an A though!
If anyone cares, it turned out to be a lot simpler than suggested in the link above, the C++ code is:

STDMETHODIMP CDObject::SaveToFile(long pDataObjectAddress, BSTR FileName)
{
      AFX_MANAGE_STATE(AfxGetStaticModuleState())

      //Copies the passed DataObject to a file
      
      HRESULT   hr = S_OK;
      IDataObject *pDataObject;
      pDataObject = (IDataObject*) pDataObjectAddress; //cast the passed pointer into a *IDataObject

      //Get the clipboard
      // Important: these strings need to be non-Unicode (don't compile UNICODE)
      unsigned short cp_format_descriptor = RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
      unsigned short cp_format_contents   = RegisterClipboardFormat(CFSTR_FILECONTENTS);



      //Set up format structure for the descriptor and contents
      FORMATETC descriptor_format =
       {cp_format_descriptor, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
      FORMATETC contents_format =  
       {cp_format_contents,   NULL, DVASPECT_CONTENT, -1, TYMED_ISTREAM};

      // Check for contents format type
      hr = pDataObject->QueryGetData(&contents_format);
      if (hr != S_OK) return hr;

      // Get the descriptor information
      STGMEDIUM storage= {0,0,0};
      STGMEDIUM ContentStorage = {0,0,0};

      contents_format.lindex = 0;
      contents_format.tymed = TYMED_ISTORAGE;
      hr = pDataObject->GetData(&contents_format, &ContentStorage);
      if (hr != S_OK) return hr;

            
      //Save the passed storage to the filename passed
      IStorage *FileStg;
      //hr = StgCreateDocfile(L"C:\\mtest\\MyMsg.msg",STGM_READWRITE + STGM_CREATE +STGM_SHARE_EXCLUSIVE ,0,&FileStg);
      hr = StgCreateStorageEx(FileName,STGM_READWRITE + STGM_CREATE +STGM_SHARE_EXCLUSIVE ,STGFMT_STORAGE ,0,0,0,IID_IStorage ,(void**)&FileStg);
      if (hr != S_OK) return hr;

      hr = ContentStorage.pstg->CopyTo(0,0,0,FileStg);
      FileStg->Commit(STGC_DEFAULT );
      FileStg->Release();
      return hr;
}

And the VB code is

Private Sub imgEMail_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim DObjectHelper As New DATAOBJECTHELPERLib.DObject
    Dim DObjectAddr As Long
    Dim MsgData() As Byte
    If (Data.GetFormat(-16370) = True) Then 'e-mail attachment
        MoveMemory DObjectAddr, ByVal ObjPtr(Data) + 16, 4 'the real IDataObject pointer is 16 bytes into the VB data object
        Call DObjectHelper.SaveToFile(DObjectAddr, "C:\TempMsg.msg")
    End If
End Sub