Reputation: 43
I have some code that send e-mails. It creates a default message then allows the user to modify it. What I would like to do is archive the message that gets sent out but any recipients that may get added. The problem is when the user clicks send the mail object gets set to null.
Public Shared Function SendRFQ(ByVal strRFQID As String, ByVal strTo As String, ByRef EmailSent As Structs.Email) As Boolean
Dim bRC As Boolean
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application)
objEmail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
With objEmail
.CC = "[email protected]"
.Subject = String.Format("RFQ")
.To = strTo
.Body = Constants.RFQ.Email.Body
.Display(True)
End With
'objEmail is null
EmailSent.To = objEmail.To
EmailSent.Subject = objEmail.Subject
EmailSent.Body = objEmail.Body
End Function
I get an COM exception; "The item has been moved or deleted."
Is there any way to accomplish this?
Upvotes: 0
Views: 1038
Reputation: 1792
You can do this by using the Send
event of the MailItem
. The following Console app shows how. You should be able to adapt it to your needs.
Imports Microsoft.Office.Interop
Module Module1
Private WithEvents objEmail As Outlook.MailItem
Sub Main()
Dim objOutlook As Outlook.Application
objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application)
objEmail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
With objEmail
.CC = "[email protected]"
.Subject = "Subject"
.To = "[email protected]"
.Body = "Body"
.Display(True)
End With
objOutlook = Nothing
End Sub
Private Sub objEmail_Send(ByRef Cancel As Boolean) Handles objEmail.Send
Console.WriteLine(objEmail.Body)
Console.WriteLine(objEmail.To)
Console.WriteLine(objEmail.Subject)
End Sub
End Module
Upvotes: 1
Reputation: 27322
You just need to keep the values of the contents of the email and populate your ByRef object:
Dim cc = "[email protected]"
Dim subject = String.Format("RFQ")
Dim body = Constants.RFQ.Email.Body
With objEmail
.CC = cc
.Subject = subject
.To = strTo
.Body = body
.Display(True)
End With
EmailSent.To = strTo
EmailSent.Subject = subject
EmailSent.Body = body
Upvotes: 0