none
Trying to retrieve emails from Outlook application in Windows Form Application(Visual Basic). RRS feed

  • Question

  • Hello,

    I'm trying to retrieve emails from an Outlook desktop application and display them into a DataGridView in my Windows Forms Application written in Visual Basic.

    Code so far(from another post aswell):

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dt As DataTable
            Try
                Dim app As Outlook.Application
                app = GetApplicationObject()
                Dim ns As Outlook.[NameSpace] = app.GetNamespace("MAPI")
                Dim inbox As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
                ns.SendAndReceive(True)
                dt = New DataTable("Inbox")
                dt.Columns.Add("Subject", GetType(String))
                dt.Columns.Add("Sender", GetType(String))
                dt.Columns.Add("Body", GetType(String))
                dt.Columns.Add("Date", GetType(String))
                DataGridView1.DataSource = dt
                For Each item As Object In inbox.Items
                    If TypeOf item Is Outlook.MailItem Then
                        Dim item1 As Outlook.MailItem = CType(item, Outlook.MailItem)
                        dt.Rows.Add(New Object() {item1.Subject, item1.Sender.Name, item1.HTMLBody, item1.SentOn.ToLongDateString() & "" + item1.SentOn.ToLongTimeString()})
                    End If
                Next
                MessageBox.Show("done")
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub

    Function GetApplicationObject() As Outlook.Application
            Dim application As Object
            If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then
                application = Marshal.GetActiveObject("Outlook.application")
            Else
                application = New Outlook.Application()
            End If
            Return application
    End Function


    Now I'm getting the following message in a messagebox when i click on Button1:

    "Open member 'Sender' for type 'MailItem' has not been found".

    Hope anyone can help.

    Thanks in advance!




    Tuesday, December 5, 2017 10:20 AM

Answers