How to get attachment's information in VC++

C

cyan21

hello,

I'm trying to get the informations from an attachment but it don't
manage to get it.
why does this piece of code doesn't work ?

CComPtr<Outlook::_Application> olapp;
CComPtr<Outlook::_MailItem> mail;
CComPtr<Outlook::Attachment> attch;
_bstr_t field;


olapp.CoCreateInstance(__uuidof(Outlook::Application), NULL,
CLSCTX_SERVER);
olapp->raw_CreateItem(Outlook::blush:lMailItem, &pDisp);

attch = mail->GetAttachments(); field = attch->GetFileName();


thanks in advance for your answers.

yann
 
D

Dmitry Streblechenko

Because MailItem.Attachments returns an instance of the Outlook.Attachments
object (note the plural), not Outlook.Attachment.
Outlook.Attachment is returns by Attachments.Item or Attachments.Add

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
C

cyan21

ok I made some modifications as you suggest me and it seems the problem
lies in the number of attachments. I always get 0 attachment whatever
the number of attachments I add

CComPtr<Outlook::Attachment> attch;
CComPtr<Outlook::Attachments> attchs;

CString tmp;
int nb;
attchs = mail->GetAttachments();

nb = (int)attchs->GetCount(); /// always returns 0
 
D

Dmitry Streblechenko

Show the code that adds the attachments. New messages (created with
Application.CreateItem) obviously have no attachments.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
C

cyan21

in fact, I add the attachments "manually" by using Outlook's interface
( not using code ).
In my program, I want to get back some informations on attachments when
I click on 'Send' button.
 
C

cyan21

as I said before, I don't add attachments by code but here is my code
to get mail's informations( it works fine for subject, body, to) :

char * MyOutlook::getMailField(int code)
{
CString tmp;
_bstr_t field;
char * s;

// for attachments
CComPtr<Outlook::Attachment> attch;
CComPtr<Outlook::Attachments> attchs;
variant_t index;
index.lVal = 1;
index.intVal = 1;


switch( code )
{
case SUJECT : field = mail->GetSubject();break;
case BODY : field = mail->GetBody();break;
case TO : field = mail->GetTo(); break;
case ATTCH_NAME : attchs = mail->GetAttachments();

if( attchs->GetCount() > 0 )
{
attch = attchs->Item(index);
field = attch->GetFileName();
}
else
return NULL;
}

// conversion type _bstr_t to CString
tmp.Format(_T("%s"), (LPCTSTR)field);

// conversion type CString to char *
s = new char[tmp.GetLength()+1];
strcpy(s,tmp);

return s;

}

and GetCount() always returns 0.
I tried with index beginning at 0 or 1 but in vain.
this method is called after I click on "Send" button.

I hope It will help you to understand my problem and maybe to solve it.
 
D

Dmitry Streblechenko

Do you mean you display the message first? What is the exact sequence of
calls? Did you try to save the message first (MailItem.Save) ?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
C

cyan21

1. I create a mailItem with code and use the method display() to show
the interface to write a mail to the user.
2. He fills the fields himself and when it's done, he pushes the Send
button
3. the interface is closed automatically after the Send button is
pressed
4. My method (see above) is called to get back informations entered by
the user.
 
C

cyan21

I call the method as soon as I detect the user pushes the send button.
I trap the "ItemSendEvent" and I noted that the send event calls the
item write and the item close event.
So I thought It was useless to call Mailitem.save. Was I wrong ?
But when I checked in Outlook, my mail is in the sentBox with the
attachments...
 
C

cyan21

ok I finally figure it out .... it's because I used a method that
copies each member of the mail and I forgot to copy
attachments.....It's so stupid.....

now I can detect my attachments but I don't manage to get an
attachment. I looked how to do in vba and try to 'translate' it in C++
but it doesn't work the object that receives the attachment is always
empty. how can it be ?


CComPtr<Outlook::Attachment> attch;
CComPtr<Outlook::Attachments> attchs;

CString tmp;
_bstr_t field;
VARIANT i;
//_variant_t i;

i.iVal = 1; // value
i.vt = VT_I4; // type of variant = integer

attchs = mail->GetAttachments();

if( attchs->GetCount() > 0 )
{

// 2 possibilities raw_Item or Item, I tried both of
them but I got the same result
//attch = attchs->Item(&i);
attchs->raw_Item(i, &attch );

// attch is empty so it crashed when I tried to get the
displayname
field = attch->GetDisplayName();
}


I tried to begin at position 0 or 1 but in vain.
 
C

cyan21

I made it but I wanted to know the pathname but it's always empty....
outlook copies the file so the path of the file is useless.


i.lVal = ....
i.vt = VT_I4
 
D

Dmitry Streblechenko

Correct PathName is only used by the attachments stored by reference. For
the regular attachments (by value), Outlook forgets the path name as soon as
the attachment is added. For all practical purposes, the attachment doesn't
even have to come from the file system.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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

Top