views:

244

answers:

1

So I'm developing an outlook add-in that basically gathers some info from the user in the form, then sends a properly formatted email to the right person. What I want to do is then make a journal entry noting that the email had been sent, and attach the email in reference.

To accomplish this manually, I can go to the "Sent Items" folder, and simply drag the email of choice down over the Journal icon in the bottom-left menu, and this accomplishes what I'm trying to do programmatically.

Here is the code I'm using once the mail is sent:

Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
    Cancel = False
    Dim sent As Outlook.MailItem = CType(Item, Outlook.MailItem)
    Dim app As New Outlook.Application

    Dim journal As Outlook.JournalItem = app.CreateItem(Outlook.OlItemType.olJournalItem)
    journal.Subject = sent.Subject & " - " & df.TextBox1.Text
    journal.Type = "E-mail Message"
    journal.Start = Now()
    journal.Duration = 0
    journal.Body = "Request sent to " & df.ComboBox4.SelectedItem.ToString()


    Try
        journal.Attachments.Add(sent, Outlook.OlAttachmentType.olByReference)
        journal.Save()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    df.Close()
End Sub

Its getting hung up at Attachments.Add and my error message is "an object could not be found". I'm guessing it means 'sent' is no good. However, I use sent.Subject to title the journal entry, and I do not get an error there. Any thoughts?

Your help is greatly valued!

A: 

I believe your error is caused by Outlook.OlAttachmentType.olByReference . Unfortunately I could not find, on google, any direct connection regarding OlAttachmentType and "an object could not be found" - so you either do the digging yourself or you provide more info.

Ando
Believe me, I've been digging for days.
Honus Wagner
so is it caused by Outlook.OlAttachmentType.olByReference or not?
Ando
Yes, I have found out that it is. So, by using Type.Missing for that param, I've been able to embed it, however, double-clicking it in the journal entry gives me a blank email as if it were created new. Any thoughts?
Honus Wagner
Apparently the email doesnt get sent until that event is complete, which is why I can't reference it. Is there an event to handle once an email has been sent?
Honus Wagner
I am no Outlook interop expert :) - but:- I don't think you can reference the email until it was sent -> you could use Type:=Outlook.OlAttachmentType.olEmbeddeditem to embed a copy of the message- after the email was sent you should be able to reference it (by getting the email from the Sent folder) -
Ando