SYMPTOMS
When you use the Microsoft Outlook Object Model and the
OnWrite extension event to save an OLE attachment in Microsoft Outlook 2002, you may receive the following error message:
0x80020009: DISP_E_EXCEPTION
Cannot save the attachments. Outlook cannot do this action on this type of attachment.
WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following is a sample code fragment to demonstrate how to programmatically save an OLE attachment in an e-mail message.
Note You cannot use the Outlook Object Model to save an OLE attachment. Instead, you must use a client extension.
// Define the dispatch ids that will be used later in the Invoke() calls.
#define Attachments 0xF815
#define AttachmentItem 81
#define AttachmentCount 80
#define GetAttachmentStorage 301
// This method is called when a message is sent.
STDMETHODIMP CMyExtension::OnWrite(IExchExtCallback* pexcb)
{
IOutlookExtCallback *poxcb=NULL;
IDispatch *pdisp=NULL;
HRESULT hr=S_OK;
// Get the Outlook IDispatch Interface.
if(FAILED(hr = GetOutlookDispatchIntf(pexcb, &poxcb, &pdisp)))
{
goto Error;
}
if(FAILED(hr = GetOutlookAtttachment(pdisp)))
{
goto Error;
}
Error:
if(pdisp!=NULL)
pdisp->Release();
if(poxcb!=NULL)
poxcb->Release();
return S_FALSE;
}
// This method will get the IDispatch Interface for Microsoft Outlook.
HRESULT GetOutlookDispatchIntf(IExchExtCallback* pexcb, IOutlookExtCallback **pprGetObj, IDispatch **ppdisp)
{
HRESULT hr = S_OK;
IUnknown* punk=NULL;
if(FAILED( hr = pexcb->QueryInterface(IID_IOutlookExtCallback,
(void **) pprGetObj)))
{
goto Error;
}
if (*pprGetObj != NULL)
{
(*pprGetObj)->GetObject(&punk);
if (punk != NULL)
{
if(FAILED(hr = punk->QueryInterface(IID_IDispatch, (void **) ppdisp)))
{
goto Error;
}
}
}
else
{
hr = E_FAIL;
}
Error:
if ( punk != NULL )
punk->Release();
return hr;
}
// This method will use the Outlook IDispatch Interface to get the attachment.
// Collect and then get the attachment data for this message.
HRESULT GetOutlookAtttachment(IDispatch *pdisp)
{
VARIANT *pVarAttachments=NULL;
VARIANT *pVarCount=NULL;
VARIANT *pVarAttachment=NULL;
VARIANT *pVarResult=NULL;
HRESULT hr=S_OK;
DISPPARAMS dispParamsNoArg={NULL,NULL,0,0};
DISPPARAMS dispParams;
VARIANTARG varg;
EXCEPINFO sExcepInfo;
if (pdisp == NULL)
{
hr = E_INVALIDARG;
goto Error;
}
pVarAttachments=(VARIANT *) malloc(sizeof(VARIANT));
pVarCount=(VARIANT *) malloc(sizeof(VARIANT));
pVarAttachment=(VARIANT *) malloc(sizeof(VARIANT));
pVarResult=(VARIANT *) malloc(sizeof(VARIANT));
// Get the Attachment collection.
if(FAILED(hr = pdisp->Invoke(Attachments,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,
&dispParamsNoArg,pVarAttachments,&sExcepInfo,NULL)))
{
goto Error;
}
// Get the number of attachments in that collection.
if(FAILED(hr = pVarAttachments->pdispVal->Invoke(AttachmentCount,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,
&dispParamsNoArg,pVarCount,&sExcepInfo,NULL)))
{
goto Error;
}
if(pVarCount->lVal==0)
{
hr = S_OK;
goto Error;
}
else
{
::VariantInit(&varg);
// Loop through each attachment
for(int i=1; i<=pVarCount->lVal; i++)
{
varg.vt=VT_I4;
varg.lVal=i;
dispParams.cArgs=1;
dispParams.rgvarg=&varg;
dispParams.cNamedArgs=0;
dispParams.rgdispidNamedArgs=NULL;
// Get the attachment.
if(FAILED(hr = pVarAttachments->pdispVal->Invoke(AttachmentItem,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD,
&dispParams,
pVarAttachment,
&sExcepInfo,
NULL)))
{
goto Error;
}
if (NULL != pVarAttachment->pdispVal)
{
// Create a temporary storage to save the attachment data.
IStorage *pOleStorage = NULL;
hr = StgCreateDocfile(L"c:\\temp\\attachment.stg",
STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED | STGM_SHARE_EXCLUSIVE,
0,
&pOleStorage);
if(SUCCEEDED(hr) && pOleStorage)
{
VARIANTARG varArgStgGet[2];
VariantInit(&varArgStgGet[0]);
VariantInit(&varArgStgGet[1]);
varArgStgGet[0].vt = VT_INT;
varArgStgGet[0].intVal = 0;
varArgStgGet[1].vt = VT_UNKNOWN;
varArgStgGet[1].punkVal = (IUnknown *)pOleStorage;
DISPPARAMS dispParamsStgGet;
dispParamsStgGet.cArgs = 2;
dispParamsStgGet.rgvarg = varArgStgGet;
dispParamsStgGet.cNamedArgs = 0;
dispParamsStgGet.rgdispidNamedArgs = NULL;
EXCEPINFO sExcepInfo;
// Get the attachment data.
if(FAILED(hr = pVarAttachment->pdispVal->Invoke(GetAttachmentStorage,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD,
&dispParamsStgGet,
NULL,
&sExcepInfo,
NULL)))
{
pOleStorage->Release();
goto Error;
}
// Do something with the storage. This storage will represent the attachment data.
pOleStorage->Release();
}
pVarAttachment->pdispVal->Release();
pVarAttachment->pdispVal = NULL;
}
}
}
Error:
if (NULL != pVarAttachments->pdispVal)
{
pVarAttachments->pdispVal->Release();
pVarAttachments->pdispVal = NULL;
}
free(pVarAttachments);
free(pVarAttachment);
free(pVarCount);
free(pVarResult);
return hr;
}