VBA - how to convert mail item attachments to mail items?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

(asking this again - don't think it got picked up as managed last time!)

We're converting from an old, creaky email system to Exchange 2007. As part
of the transition, we need the old system to forward to Exchange. However,
when it does so the mails forwarded come as attachments.

Is there a VBA way of converting these attachments to emails? It seems one
can drag-and-drop them into mail folders, but I can't find a way of doing
this in VBA
 
Or, better yet, use Redemption. Importing them with CreateItemFromTemplate won't preserve the original sender and recipient information.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Michael,

That sort of works - but I then get a message that's a draft ready to send
not a received message which is where I want to be. Code is:

Option Explicit
Public olNS As Outlook.NameSpace
Public olInbox As Outlook.MAPIFolder
Public strTemp As String
Public objFSO As Variant
Const cstrTempName = "\temp.msg" ' name of temp file - location is TEMP if
set, root if not

Sub UnpackTWForward(miMail As Outlook.Mailitem)
'
' UnpackTWForward - Convert forwarded mail attachments into real mail
'
' Author - Steve Durbin 2007-03-08 "durbis" %at% ^bridgend.^ ^gov.^
^uk^
'
' Notes - Should be run on back of OL rule picking
' up the mail that you know to be forwarded
' e.g. mail from your old system address
'
Dim strID As String ' the MAPI item string
Dim olNewMail As Outlook.Mailitem ' New mail item
Dim olAttachment As Outlook.Attachment ' old mail item's mail attachment
Dim olMail As Outlook.Mailitem ' old mail item

' first time called? Inititalise the recurrent stuff
If olInbox Is Nothing Then
Set olInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set olNS = Application.GetNamespace("MAPI")
If Environ("TEMP") = "" Then
strTemp = Environ("SYSTEMROOT") + "\temp.msg"
Else
strTemp = Environ("TEMP") + "\temp.msg"
End If
End If

' Why do it this way? To bypass the security warning message
strID = miMail.EntryID
Set olMail = olNS.GetItemFromID(strID)

' if it's a real forward will be one attachment of mail item type
If olMail.Attachments.Count > 1 Then
Exit Sub
End If
' okay, has one attachment. Try to import it as a message...
Set olAttachment = olMail.Attachments(1)
olAttachment.SaveAsFile strTemp
' now, if it's NOT an email attachment, the next call will fail...
On Error Resume Next
Set olNewMail = Application.CreateItemFromTemplate(strTemp)
On Error GoTo 0
' ...and olnewmail will not be an object...
If olNewMail Is Nothing Then
Else
' yep, we had a mail - process the rest.
olNewMail.UnRead = True

olNewMail.Save
olNewMail.Move olInbox
olMail.Delete
End If
objFSO.deletefile strTemp
End Sub
 
Sue,

Looked at redemption before posting; it's a possibility, but I was hoping
for a no-extra-bits solution as it's quite a big rollout here (2.5k users).
If I gotta do it, I gotta, but there *must* be a MS-only solution...mustn't
there?
 
The MS-only solution would involve Extended MAPI, which means C++, not VBA.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Hi Steve,

If I understood properly, you are searching a way to use VBA to convert the
e-mail attachments to e-mail message. Is that correct?

This appears to be a coding/development related request. For assistance
with this coding issue, please consider these resources:

1. MSDN newsgroups for peer experiences and recommendations:

http://msdn.microsoft.com/newsgroups/default.asp.

2. Public newsgroups for peer experiences and recommendations:

microsoft.public.office.developer.vba


Regards,

Leon Hao

Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
====================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top