Getting the attachments in MailItem

  • Thread starter nikolayds via OfficeKB.com
  • Start date
N

nikolayds via OfficeKB.com

Hi all,
I am new in outlook addin programming, so naturally I am getting a setback
right away :).
Problem: need to get the attachaments of email

Code:

(Version one - no Redemption)
Code:
Outlook::Attachments* na = pMailItem->Attachments;

int count = na->Count;
Outlook::Attachment* att;
_variant_t inx =0;
for(int t=1;t<=count; t++)
{
inx = t;
att = na->Item(inx);
int type = att->Type;
CString filename = (BSTR) att->FileName;
}
This one is getting the first attachment but blows on subsequent with read
violation error.

Using redemption
Code:
IAttachmentsPtr spAttachments = spSafeMailItem->Attachments;
for(int j=1; j<=spAttachments->Count; j++)
{


HRESULT _thr;
_variant_t inx = j;

IAttachmentPtr pAttachment;
_thr = spAttachments->raw_Item(inx, &pAttachment);//->get__Item(inx,
&pAttachment);
// getting E_UNEXPECTED
if(_thr!=S_OK)
{
continue;
}
long size = pAttachment->FileSize;
CString fname = (BSTR) pAttachment->FileName;
if(pAttachment->Type == 1) //ByValue
{
CString path = "C:\\";
path.Append(fname);
_thr = pAttachment->SaveAsFile(path.AllocSysString());
if(_thr==S_OK)
{
int a = 0;
}
}
}

Any ideas what I am doing wrong?
 
N

nikolayds via OfficeKB.com

I got it: using the following code instead


hr = pAttachments->raw_Item(_variant_t(j),&pAttachment);
if (hr!=S_OK)
continue;

pAttachment->Release();
_bstr_t pAttachmentName;
pAttachment->get_FileName(pAttachmentName.GetAddress());


Hi all,
I am new in outlook addin programming, so naturally I am getting a setback
right away :).
Problem: need to get the attachaments of email

Code:

(Version one - no Redemption)
Code:
	Outlook::Attachments* na = pMailItem->Attachments;

	int count = na->Count;
	Outlook::Attachment* att;
	_variant_t inx =0;
	for(int t=1;t<=count; t++)
	{
		inx = t;
		att = na->Item(inx);
		int type = att->Type;
		CString filename = (BSTR) att->FileName;
	}
This one is getting the first attachment but blows on subsequent with read
violation error.

Using redemption
Code:
IAttachmentsPtr spAttachments = spSafeMailItem->Attachments;
	for(int j=1; j<=spAttachments->Count; j++)
	{


		HRESULT _thr;
		_variant_t inx = j;

		IAttachmentPtr pAttachment;
		_thr = spAttachments->raw_Item(inx, &pAttachment);//->get__Item(inx,
&pAttachment);
		// getting E_UNEXPECTED
if(_thr!=S_OK)
		{
			continue;
		}
		long size = pAttachment->FileSize;
		CString fname = (BSTR) pAttachment->FileName;
		if(pAttachment->Type == 1) //ByValue
		{
			CString path = "C:\\";
			path.Append(fname);
			_thr = pAttachment->SaveAsFile(path.AllocSysString());
			if(_thr==S_OK)
			{
				int a = 0;
			}
		}
	}

Any ideas what I am doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top