Tips and tricks for .NET using ASP and VB code.

Get Outlook folders and email messages

Public Class ThisAddIn
    Public WithEvents PublicOutlookInspectors As Outlook.Inspectors

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        PublicOutlookInspectors = Me.Application.Inspectors
        Dim OutlookApplication As New Outlook.Application
        Dim OutlookNameSpace As Outlook._NameSpace = CType(OutlookApplication.GetNamespace("MAPI"), Outlook._NameSpace)
        Dim OutlookMapiFolder As Outlook.MAPIFolder = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent
        'Dim OutlookExplorer As Outlook._Explorer = OutlookMapiFolder.GetExplorer(False)
        OutlookNameSpace.Logon(String.Empty, String.Empty, False, True)
        Dim OutlookMapiFolderItems As Outlook.Items = OutlookMapiFolder.Items
        GetFoldersRecursive(OutlookMapiFolder)
    End Sub

    Public Sub GetFoldersRecursive(OutlookMAPIFolder As Outlook.MAPIFolder)
        If OutlookMAPIFolder.Folders.Count = 0 Then
            System.Diagnostics.Debug.WriteLine("Folder: " & OutlookMAPIFolder.FullFolderPath)
            For Each OutlookMAPIFolderItem As Object In OutlookMAPIFolder.Items
                If TypeOf (OutlookMAPIFolderItem) Is Outlook.MailItem Then
                    System.Diagnostics.Debug.WriteLine("Subject: " & OutlookMAPIFolderItem.Subject)
                End If
            Next OutlookMAPIFolderItem
        Else
            For Each OutlookMAPISubFolder As Outlook.MAPIFolder In OutlookMAPIFolder.Folders
                GetFoldersRecursive(OutlookMAPISubFolder)
            Next OutlookMAPISubFolder
        End If
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub PublicOutlookInspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles PublicOutlookInspectors.NewInspector
        Dim OutlookMailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)
        If OutlookMailItem IsNot Nothing Then
            If OutlookMailItem.EntryID Is Nothing Then
                OutlookMailItem.Subject = "This text was added by using code"
                OutlookMailItem.Body = "This text was added by using code"
            End If
        End If
    End Sub

End Class

No comments:

Post a Comment

Search This Blog