Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I would like to get Outlook opened email body. Using code below, I managed to get pDispatch as _MailItem type. The problem is, it fails when I tried to get email body:

hr = pMailItem->get_Body(&messageBody); //failed here 


Need suggestions from you guys. Below is snippet of my code:

C++
case 0x0000fba7:
			if(pDispParams->cArgs <= 0)
				return E_INVALIDARG;
			else
			{
				HRESULT hr;
				IDispatch *pDispatch = pDispParams->rgvarg[0].pdispVal;
				BSTR szName;
				
				if(pDispatch)
				{
					LPTYPEINFO lpMyTypeInfo = NULL;
					hr = pDispatch->GetTypeInfo(0,0,&lpMyTypeInfo);
					if(lpMyTypeInfo != NULL)
						hr = lpMyTypeInfo->GetDocumentation(MEMBERID_NIL,&szName,0,0,0);
				}

				if (0 != strcmp("_MailItem",(char *)(_bstr_t)szName)) //confirmed
					break;

				Outlook::_MailItemPtr pMailItem = NULL;
				pDispatch->QueryInterface(__uuidof(_MailItemPtr), (void**)&pMailItem);

				if (pMailItem)
				{
					BSTR messageBody;
					hr = pMailItem->get_Body(&messageBody); //failed here
					
					if(FAILED(hr))
					{
						//hr returned error code seems random everytime i debug
					}
				}
			}
			break;
Posted
Updated 16-Dec-14 0:42am
v4
Comments
Jochen Arndt 16-Dec-14 6:29am    
The returned error code may help identifying the problem.
kinanda 16-Dec-14 6:43am    
HRESULT hr returned error code seems random everytime I debug. Thanks for your suggestion.
Jochen Arndt 16-Dec-14 7:57am    
This is rather uncommon. You should nevertheless post some of such codes to check if they are really random or maybe COM error codes.

You may also print them to the debug window just after returning from each function using the TRACE macro.

1 solution

Solved by getting Outlook using Explorer. Cannot get mail using previous way

C++
IDispatch *pDispatch = pDispParams->rgvarg[0].pdispVal //can't use this


C++
case 0x0000fba7:
			if(pDispParams->cArgs <= 0)
				return E_INVALIDARG;
			else
			{
				Outlook::_ApplicationPtr spApp("Outlook.Application");
				Outlook::_MailItemPtr spMailItem;
				IDispatch *pDispatch;
				HRESULT hr;
				long lll;
				variant_t vvv = 1;

				if (NULL == spApp)
				{
					hr = spApp.CreateInstance("Outlook.Application");
				}

				if (spApp)
				{
					Outlook::_ExplorerPtr spExplorer;
					hr = spApp->ActiveExplorer(&spExplorer);
					if (spExplorer)
					{
						Outlook::SelectionPtr spSel;
						hr = spExplorer->get_Selection(&spSel);
						hr = spSel->get_Count(&lll);
						if (spSel &&  lll == 1) // if only 1 selection
						{
							hr = spSel->Item(vvv, &pDispatch);
							pDispatch->QueryInterface(__uuidof(_MailItemPtr), (void**)&spMailItem);
							BSTR bbb;
							hr = spMailItem->get_Body(&bbb); //get email body
						}
					}
				}
			}
			break;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900