Error deleting second attach from RTF Msg w/ Outlook 2007

B

Byron

I get an error, 0x80030002 "%1 could not be found.", when I try to
delete the second attachment from an rtf email message only in Outlook
2007. The first attachment gets processed with no problems. I've
tried getting a new Outlook::AttachmentsPtr and a new
Outlook::AttachmentPtr with no luck. The same code works for HTML and
Plain Text for Outlook 2007 as well as HTML, Plain Text, and RTF for
Outlook 2002 and 2003. Below is a snippet of code. Thanks.

pAttachment = pAttachments->Item(1);

m_csInputFile = m_csOutputPathFile = filepath;

originalFilename = (TCHAR*) pAttachment->GetFileName();

m_csInputFile += originalFilename;
m_csOutputPathFile += outfilename;

//Lets try and delete the files just in case the same filename exists
in the temp dir.
DeleteFile(m_csOutputPathFile);
DeleteFile(m_csInputFile);

BSTR bstr = m_csInputFile.AllocSysString();

if (FAILED(pAttachment->SaveAsFile(bstr)))
{
// If we can't save the attachment, we can't encrypt.
CString errMsg;
errMsg.Format(_T("An error was encountered when trying to encrypt
attachment %s.-*"), originalFilename);
AfxMessageBox(errMsg);
throw;
}
SysFreeString(bstr);

// It fails here on the second iteration.
if (FAILED(pAttachment->Delete()))
{
// If we can't remove the "clear text" attachment, we can't send.
CString errMsg;
errMsg.Format(_T("An error was encountered when trying to encrypt
attachment %s."), originalFilename);
AfxMessageBox(errMsg);
}
 
B

Byron

Could someone w/ a known good add-in test this out? I've stripped out
all the code in the Send event except for what's neccessary for
deleting attachments and it still fails with 0x80030002 "%1 could not
be found.". Here is what I have in the send event.

case 0xf005: // Send
try
{
Outlook::_MailItemPtr mailPtr = NULL;
mailPtr = m_pItem->GetMailItem();
int attCount = mailPtr->GetAttachments()->GetCount();
for (int i = attCount; i > 0 ; i--)
{
mailPtr->GetAttachments()->Item(i)->Delete();
}
}
catch(...)
{
CString errMsg;
errMsg.Format(_T("An error was encountered when trying to --Delete--
attachment"));
AfxMessageBox(errMsg);
}
 
B

Byron

Finally found a solution to the problem. Simple call Save on the
MailItem before trying to process the next attachment.

case 0xf005: // Send
try
{
Outlook::_MailItemPtr mailPtr = NULL;
mailPtr = m_pItem->GetMailItem();
int attCount = mailPtr->GetAttachments()->GetCount();
for (int i = attCount; i > 0 ; i--)
{
mailPtr->GetAttachments()->Item(i)->Delete();
mailPtr->Save();
}
}

catch(...)
{
CString errMsg;
errMsg.Format(_T("An error was encountered when trying to --
Delete--
attachment"));
AfxMessageBox(errMsg);

}
 

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