Click here to Skip to main content
13,146,402 members (44,911 online)
Rate this:
Please Sign up or sign in to vote.

I m trying to develop outlook plugin using ATL/COM in c++. I want to get the MailItem methods to access the outlook mail so that i can get the desired results like TO, CC, BCC, Subject, Body and attachments.....till now i have got everything accept attachments here is my code for getting attachments

CComPtr<Outlook::_MailItem> MailPtr;
CComQIPtr<Outlook::Attachments> spAttachments;
CComQIPtr<Outlook::Attachment> spAttachment;

long count;

MailPtr->get_Attachments (&spAttachments);
spAttachments->get_Count (&count);

for (;count--; )
    VARIANT index;
    index.iVal = 1; // value
    index.vt = VT_I2; // type of variant = integer
    BSTR StoragePtr;
    spAttachments->Item (index, &spAttachment);
    spAttachment->get_FileName (&StoragePtr);
    MessageBoxW (NULL, (LPCWSTR)StoragePtr, L"Attached File Name", MB_OK);


The above code works fine for only one attachment but if there are more than one attachments it is getting crash when it comes for the second time in the for loop here at this line it is getting crash

spAttachments->Item (index, &spAttachment);

i m not able to get the file name of the second attachment so please anyone help me to this problem.

Thanks in advance.
Posted 6-Sep-12 1:15am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I wonder how you solve the problem? I met it recently.
Is it a bug?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Oh, I've found a way to figure it out!!
Just Remove : CComQIPtr<outlook::attachment xmlns:outlook="#unknown"> spAttachment;
into for loop.

I think method "Item" refuse to overwrite spAttachment when he found it's not NULL

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 17 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100