How To Write RTF from RichEditControl into PR_RTF_COMPRESSED (255621)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)
  • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q255621

SUMMARY

A Microsoft Foundation Classes (MFC) CRichEditCtrl class can be used as a source of data for the PR_RTF_COMPRESSED property of an Extended MAPI message. The sample code in the "More Information" section of this article demonstrates one way to write the RTF stream out from the control into the property.

MORE INFORMATION

//This code assumes that m_lpMessage is a class level variable
//pointing to an existing MAPI message.
class MyRichEditCookie 
{
public:
LPSTREAM  m_pData;
MyRichEditCookie(LPSTREAM pData ) 
{ 
    m_pData=pData; 
}

};

static DWORD CALLBACK EditStreamCallBack(DWORD dwCookie, LPBYTE pbBuff,
            LONG cb, LONG *pcb)
{
    HRESULT hRes = S_OK;
    ASSERT(pbBuff);
    ASSERT(pcb);
    MyRichEditCookie * pCookie = (MyRichEditCookie *)dwCookie;
    ASSERT(pCookie);
    
    LPSTREAM stmData = pCookie->m_pData;
    DWORD dw = 0;
    ULONG cbWritten;
    
    hRes = stmData->Write(pbBuff,cb,&cbWritten);
    //TODO: Check hRes and complain appropriately if not S_OK
    
    *pcb = cbWritten;
    
    return dw;
}

void CRichEdDlg::OnCommit() 
{
    
    HRESULT hRes = S_OK;
    
    LPSTREAM lpStm = NULL;
    LPSTREAM lpStmUncompressed = NULL;
    
    hRes = m_lpMessage->OpenProperty(   PR_RTF_COMPRESSED,  
        &IID_IStream,  
        STGM_WRITE | STGM_DIRECT,  
        MAPI_CREATE | MAPI_MODIFY,
        (LPUNKNOWN *)&lpStm);

    if (SUCCEEDED(hRes))
    {
        hRes = WrapCompressedRTFStream(lpStm, MAPI_MODIFY,
            &lpStmUncompressed);
    }
    
    if (SUCCEEDED(hRes))
    {
        LONG cb;
        EDITSTREAM es = {0, 0, EditStreamCallBack};
        MyRichEditCookie  cookie(lpStmUncompressed);
        es.dwCookie = (DWORD)&cookie;
        UINT uFormat = SF_RTFNOOBJS;
        cb = m_RichEditCtrl.StreamOut(uFormat,es);
        hRes = lpStmUncompressed->Commit(STGC_DEFAULT);

    }
    if (SUCCEEDED(hRes))
    {
        hRes = m_lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
    }
    //TODO: Check hRes and complain appropriately if not S_OK

    if (lpStm) lpStm->Release();
    if (lpStmUncompressed) lpStmUncompressed->Release();
    
}
				

Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbhowto kbMsg KB255621