Ed Mozley
Ed Mozley

Reputation: 3539

Visual Basic move outlook sent items to other folder

I have installed Visual Studio 2017 Pro and am having a go at writing a VSTO Outlook add-in. Essentially I want to execute code when a new item is added to the sent items folder. I have been researching and apparently this is the most efficient way of doing this rather than having code on the ItemSend event.

I have added various MsgBox commands so I can see that various bits are actually running but for some reason the ItemAdd event does not seem to be firing. My code is as follows:

Public Class ThisAddIn
    Public WithEvents myOlItems As Outlook.Items

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        MsgBox("Initliasing add-in")
        AddIn_Init()
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub AddIn_Init()
        myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail).Items
        MsgBox("We are ready!")
    End Sub

    Private Sub myOlItems_ItemAdd(ByVal Item As Object)
        MsgBox("You sent an email from " & Item.SenderEmailAddress)
    End Sub

End Class

I think I am close to it working but am not quite there yet - any pointers very gratefully received.

Upvotes: 0

Views: 378

Answers (1)

Ed Mozley
Ed Mozley

Reputation: 3539

Final code is now working - thank you!

Public Class ThisAddIn
    Public WithEvents myOlItems As Outlook.Items

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        MsgBox("Initliasing add-in")
        AddIn_Init()
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub AddIn_Init()
        myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail).Items

        MsgBox("We are ready!")
    End Sub

    Private Sub myOlItems_ItemAdd(Item As Object) Handles myOlItems.ItemAdd
        MsgBox("You sent an email from " & Item.SenderEmailAddress)
    End Sub
End Class

Upvotes: 0

Related Questions