Microsoft KB Archive/255621

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


How To Write RTF from RichEditControl into PR_RTF_COMPRESSED

Article ID: 255621

Article Last Modified on 8/25/2005



APPLIES TO

  • Microsoft Messaging Application Programming Interface
  • Microsoft Visual C++ 6.0 Service Pack 5



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();
    
}
                

Keywords: kbhowto kbmsg KB255621