1
votes

I need to create a mail via VBA, send it, and then export a .msg file of the sent mail to be archived (I know, it's weird, but that's what the boss asked for).

Creating the mail is straightforward:

Set OLK = Outlook.Session
Set ML = OLK.Createitem olMailItem
With ML 
 .Recipients.add "[email protected]"
 .Subject = "Great mail you have there"
 .Body = "It would be a shame if somebody couldn't archive it"
 End with 

ML.Send

Problem is, after sending the mail is moved in the sent folder and the ML object points to nothing.

I could use the .saveas method before sending, but then the saved file is the unsent version, which can be edited and sent again.

How can I trace the mail in the sent folder?

The "brute force" way I found out implies saving the ConversationIndex before sending

IDX= ML.ConversationIndex 

and then scan all the items in the sent folder for it:

For each ML in OLK.Session.GetDefaultFolder(olFolderSentMail).Items 
    If ML.ConversationIndex = IDX Then ML.SaveAs HomeDir & "\" & OutFileName: Exit For
Next

but it isn't exactly a smooth work.... (and may fail if some smartass replies to the automatic mail, even if nobody should)

2

2 Answers

1
votes

Max,

You can handle the ItemAdd event of the Items class which belongs to the Sent Items folder. It is fired when one or more items are added to the specified collection.

In the ItemAdd event handler you can check out whether a particular item should be saved or not. For example, you can add a user property before calling the Send method. See the UserProperties class for more information.

 Set myProp = myItem.UserProperties.Add("MyPropName", olText)

Be aware, the MailItem class provides the SaveSentMessageFolder property which allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. So, you can assign a custom folder to save them.

0
votes

You can add a user property (MailItem.UserProperties.Add), but that would cause the message to be sent in the TNEF format unless the UseTnef property (DASL name http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8582000B) is explicitly set to false.

You can set a named MAPI property using PropertyAccessor.SetProperty - just pick your custom GUID and property name. E.g.

ML.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{3ADE3813-37A9-49C9-AD84-D49C8FF5D660}/MyOwnProp", "SomeValue")