Modifying Attachment contents using VC++ ATL

F

furrbie

Hi,

I am trying to develop an outlook 2002 add-in using VC++ ATL. I have added a
button in the New Mail Message inspector window and I am trying to add the
following functionality:

Every time the user clicks the button, modify the attachment that he has
inserted in the mail message (assuming he has already done so). My code so
far:

CComQIPtr<Outlook::_MailItem>spMailItem;
CComQIPtr<Outlook::Attachments> spAttachments;
CComQIPtr<Outlook::Attachment> spAttachment;

BSTR bstrPathName;
ATLASSERT(spMailItem);

VARIANT index;
index.iVal = 1; // value
index.vt = VT_I2; // type of variant = integer

spMailItem->get_Attachments(&spAttachments);
ATLASSERT(spAttachments);

long count;
spAttachments->get_Count(&count);

if (count > 0) {
spAttachments->Item(index, &spAttachment);

ATLASSERT(spAttachment);
spAttachment->get_PathName(&bstrPathName);
}

My problem is that displaying "bstrPathName" gives me an empty string. I
have tried get_DisplayName() and was able to display the filename correctly.

Am I right in guessing that Outlook does not keep the full path once the
file has been inserted?

If this is the case, how should I go about modifying the file contents?

Thanks in advance,
Mark
 
K

Ken Slovak - [MVP - Outlook]

Save the attachment to the file system and work on the file that way, then
attach it again. You also should probably save the item before working with
the attachments, some things change on attachments after the container item
is saved.
 
F

furrbie

Hi,

Thank you for the reply. I am currently trying out your suggestion.

One more question: are there any sample codes on how to use the
spAttachments->Add() method. I can't seem to get it to work correctly.

Thanks,
Marc
 
K

Ken Slovak - [MVP - Outlook]

There are lots of examples but few or none for C++ most likely. What problem
are you having, bearing in mind that I'm not a C++/ATL programmer.
 
F

furrbie

I tried the following code:

spMailItem->get_Attachments(&spAttachments);
ATLASSERT(spAttachments);

long count;
BSTR bstrFilename = SysAllocString(L"c:/test.txt");
CComVariant filename(bstrFilename);

spAttachments->get_Count(&count);
spAttachments->Add(filename, CComVariant((short)olByValue),
CComVariant((short)count), CComVariant(), &spNewAttachment);

The attachment "test.txt" seems to be added but the new mail message
inspector does not show the attachment icon in the window.

If I manually add another attachment using the 'Insert' button, the window
will show my attachment as well the new one.

If I try to send the message without adding another attachment, I get some
memory error.

Thanks,
Mark
 
K

Ken Slovak - [MVP - Outlook]

If you add the attachment in code you won't see it unless you first save the
item before adding the attachment and then again after, and you still might
not see it unless the item was released and closed and then re-opened.
Things are different with code and the user interface.
 
F

furrbie

Please ignore my previous post. It seems I wrote the Add method wrongly (I
should have passed in a display name as one of the arguments).
 

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