Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically Unpack Attached Messages
    primarykey
    data
    text
    <p>I have a secondary exchange account where a server rule is active that forwards every mail it receives to my primary account (on another server). To avoid pointless forwarding headers and to preserve the From and To fields, I forward mails as an attachment and </p> <p>I have three issues with this code and am a bit stuck, so I'm posting it here to hopefully get some input:</p> <ul> <li>I'd like to run attachment validation so only actual message types are unpacked to the Inbox. I've found the <code>.Type</code> property but this only gives me a number and I can't find the corresponding reference. If any non-message attachments (or no attachments) are found, the forwarding message should be saved or not deleted.</li> <li>Items are created in the Inbox as drafts instead of received mail items. I can't find any way to change the document type.</li> <li>It seems like my code randomly creates empty messages in my Outbox. Perhaps this is due to opening the message from disk and not doing anything with it apart from moving it, but I can't really be sure right now. If an unpacked message has attachments, an empty draft with those attachments can be found in the Outbox.</li> </ul> <p>Below I've posted the entire code, created largely thanks to information from an answer to <a href="https://stackoverflow.com/a/18902849/1103571">a related question</a>. </p> <pre><code>Public Sub unpackAttachedMessage(itm As Outlook.MailItem) Dim olApp As New Outlook.Application Dim olNameSpace As Outlook.NameSpace Dim olTargetFolder As Outlook.Folder Dim objAtt As Outlook.Attachment ' Program Configuration Variables and Constants Const saveFolder As String = "C:\Temp\Outlook" Const messageCategory As String = "CategoryName" ' Runtime Variables Dim i As Integer Dim attachmentCount As Integer i = 1 Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") ' Folder creation does not seem to work. If Not fso.FolderExists(saveFolder) Then fso.CreateFolder (saveFolder) End If ' For each attachment in the message. For Each objAtt In itm.Attachments ' Save it to disk as a message. objAtt.SaveAsFile saveFolder &amp; "\" &amp; i &amp; ".msg" ' Retrieve a message from disk. Dim message As Outlook.MailItem Set message = Application.CreateItemFromTemplate(saveFolder &amp; "\" &amp; i &amp; ".msg") ' Modify the Message. ' Note that this and potentially other message options need ' to be set BEFORE you move the item to its destination folder. ' Set the Category. message.Categories = message.Categories &amp; "," &amp; messageCategory ' Mark as unread. message.UnRead = True ' MsgBox "Class: " &amp; itm.MessageClass &amp; " --- Attached Item Class: " &amp; message.MessageClass ' Doesn't work 'message.MessageClass = olPostItem ' Save changes to the message. message.Save ' Move the item to Inbox. Set olNameSpace = olApp.GetNamespace("MAPI") Set olTargetFolder = olNameSpace.GetDefaultFolder(olFolderInbox) message.Move olTargetFolder ' objAtt.DisplayName Set objAtt = Nothing i = i + 1 Next attachmentCount = i End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload