how to get selected from outlook mail message

Joined
Jun 24, 2009
Messages
1
Reaction score
0
Hello Everyone,

I have custom tollbar with a button in outlook 2003 main explorer. Onclick of this button, i want to get/grab the selected text of the current mail message. I use vc++ to create my addin.

Below is the code snippet i tried and am getting "unspecified error" while creating the range.

Its a urgent requirement.Can you please suggest me a way to get it working..

-----------------------
Outlook::SelectionPtr spSlctnPtr;
Outlook::_MailItemPtr spMlItmPtr;
Outlook::_InspectorPtr spInspctrPtr;
MSHTML::IHTMLDocument2Ptr spHtmlDocPtr;
MSHTML::IHTMLSelectionObjectPtr spHtmlSlctnObjPtr;
MSHTML::IHTMLTxtRangePtr spHtmlTxtRngePtr;
MSHTML::IHTMLElementPtr spHtmlElemPtr;
Word::_DocumentPtr spDocPtr;
Word::SelectionPtr spWrdSlctnPtr;
HRESULT hr = S_OK;
int nCnt = 0;
CComVariant vIdx;
LPDISPATCH lpDisp = NULL;
spSlctnPtr = m_spExplrPtr->GetSelection();
nCnt = spSlctnPtr->Count;
for( int i =1 ; i <= nCnt; i++ )
{
vIdx = i;
lpDisp = spSlctnPtr->Item( vIdx );
if( lpDisp == NULL ) continue;
hr = lpDisp->QueryInterface( __uuidof( Outlook::_MailItemPtr), (void**)&spMlItmPtr );
spInspctrPtr = spMlItmPtr->GetInspector;
if( spInspctrPtr->IsWordMail() == VARIANT_TRUE )
{
spDocPtr = spInspctrPtr->WordEditor;
spWrdSlctnPtr = spDocPtr->Application->Selection;
AfxMessageBox((CString)spWrdSlctnPtr->Text);
}
else if( ( lpDisp = spInspctrPtr->HTMLEditor ) != NULL )
{
hr = lpDisp->QueryInterface( __uuidof( MSHTML::IHTMLDocument2),(void**)&spHtmlDocPtr);
if( FAILED(hr) ) return S_FALSE;

/* //tried this logic too - but didnt work :(
// get the body element of the document
spHtmlElemPtr = spHtmlDocPtr->body;
// retrieve all the HTML content of the body object
BSTR bstr;
hr = spHtmlElemPtr->get_innerHTML( &bstr );
AfxMessageBox((CString)bstr);
return S_OK;
*/

spHtmlSlctnObjPtr = spHtmlDocPtr->selection;
_bstr_t bsType = spHtmlSlctnObjPtr->type;
lpDisp = NULL;
hr = spHtmlSlctnObjPtr->raw_createRange( &lpDisp );
if( FAILED(hr) || lpDisp == NULL ) return S_FALSE;
hr = lpDisp->QueryInterface( __uuidof( MSHTML::IHTMLTxtRange),
(void**)&spHtmlTxtRngePtr);
if( FAILED(hr) || spHtmlTxtRngePtr == NULL )return S_FALSE;
AfxMessageBox( (CString)spHtmlTxtRngePtr->Gettext().GetBSTR());
}
}
-----------------------
Thanks in advance,
S.Jayashree
 
Back
Top