views:

104

answers:

2

Hi All,

I have code, similar to the following, that I would like to modify:

Sub SendEmail()

    Dim myOlApp As Outlook.Application
    Dim myItem As Outlook.MailItem

    'Create an Outlook application object
    Set myOlApp = New Outlook.Application

    'Create a new MailItem form
    Set myItem = myOlApp.CreateItem(olMailItem)

    'Build and display item
    With myItem
                .To = “[email protected]”
                .Subject = “Test Subject”
                .HTMLBody = “Test Body”
                .Display
                .SaveAs “C:\Test.msg”, olMSG   
    End With

End Sub

This code is called from various buttons throughout the application. When a button is clicked, a new email is created and saved. Unfortunately, the email is saved as soon as it is created and BEFORE it is sent... so, if any modifications are made to it, they will not be in the saved version.

What can I do to modifiy this code to ONLY save the email once it has been sent?

Feel free to ask any followup questions as necessary and I will respond as best I can.

Thanks!

Robert

+2  A: 

You can use Outlook events with Access. For this example you will need a Class Module called clsOlMail with this code:

''Requires reference to the Microsoft Outlook x.x Object Library
Dim WithEvents conItems As Outlook.Items

Private Sub Class_Initialize()
   Set oApp = Outlook.Application
   Set oNS = oApp.GetNamespace("MAPI")
   Set conFolder = oNS.GetDefaultFolder(olFolderSentMail)
   Set conItems = conFolder.Items
End Sub

Private Sub Class_Terminate()
   Set conItems = Nothing
   Set conFolder = Nothing
   Set oNS = Nothing
   Set oApp = Nothing
End Sub

Sub ConItems_ItemAdd(ByVal Item As Object)
Dim frm As Form

   Set frm = Forms!frmEmailDetails

   frm.txtSenderName = Item.SenderName
   frm.txtSentOn = Item.SentOn
   frm.txtTo = Item.To
   frm.txtCreationTime = Item.CreationTime
   frm.txtBCC = Item.BCC
   frm.txtCC = Item.CC
   frm.txtSentOnBehalfOfName = Item.SentOnBehalfOfName
   frm.txtSubject = Item.Subject
   frm.txtBody = Item.Body
End Sub

You will also need a form called frmEmailDetails with these textboxes:

txtSenderName, txtSentOn, txtTo, txtCreationTime, txtBCC, txtCC, txtSentOnBehalfOfName, txtSubject, txtBody

And this code:

Private oEvent As clsOLMail
''Requires reference to Microsoft Outlook x.x Object Library

Public oApp As Outlook.Application
Public oNS As Outlook.NameSpace
Public conFolder As Outlook.MAPIFolder

Private Sub Form_Open(Cancel As Integer)
   Set oEvent = New clsOlMail
End Sub

Open the form and send an email through Outlook, you can use one of the examples shown above. The form fields should fill with the relevant details from the sent email. You are likely to get an Outlook security warning.

From: http://wiki.lessthandot.com/index.php/Access%5Fand%5FEmail

Remou
And this will save the email after it has been sent? I don't see that anywhere in your code example...
Robert
It utilizes Outlook events. Consider these two lines in the class module: Set conFolder = oNS.GetDefaultFolder(olFolderSentMail) Set conItems = conFolder.Items
Remou
The code will not save the email after it is sent as it stands, however, it should be easy enough to add a line or two.
Remou
A: 

The problem is there is no EntryID for the newly created item. Once you save/send this item, the reference is no longer good. Why is probably due to how MAPI works. Remou suggests using the ItemAdd event to handle the item newly added to the special folder "Sent Items". From this event you can save the message. The only issue I see is how would you know passed item is the sent item. You are calling Display, which allows the user to preview, edit, send, or close the message without sending. Therefore, the item may not be the mail item you created. To get around this, add a custom property to your mail item. When the ItemAdd event is fired, you can inspect the passed item for the custom property, and save if needed.

AMissico