locked
Export emails from outlook email into windows form application RRS feed

  • Question

  • Hello,

    I'm trying to make a Windows Form Application which can retrieve/export emails from Outlook email to a tableview in my Windows Form Application.

    I got the Outlook desktop application installed on the computer im programming with.

    Since I have no clue how to approach this, can anyone give me a small start up to acces emails or something?

    Thanks.

    Thursday, November 30, 2017 10:11 AM

Answers

  • Hi Niels,

    You said that you want to export emails into TableView, but I can not search this control in Winform, is it a control? I do one example that exporting emails to datagridview, you can take a look.

     Dim dt As DataTable
            Try
                Dim app As Outlook.Application = New Outlook.Application()
                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, 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

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Niels Schutte Tuesday, December 5, 2017 10:16 AM
    Friday, December 1, 2017 3:10 AM

All replies

  • Niels,

    Try this forum, that is made for your question. (As long as you use MS-Office Outlook as I presume from your text).

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto


    Success
    Cor

    Thursday, November 30, 2017 10:30 AM
  • I want to create a Windows Form Application with a table view, this table view has to be filled in with emails im getting from my outlook email. I've posted the question also on the forum you mentioned. Thanks.
    Thursday, November 30, 2017 1:22 PM
  • Use Microsoft.Office.Interop.Outlook

    Something like this =>

    Dim oa As Outlook.Application
    If (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0) Then
        oa = Marshal.GetActiveObject("Outlook.Application")
    Else
        oa = New Outlook.Application()
    End If
    Dim ons As Outlook.NameSpace = oa.GetNamespace("MAPI")
    Dim emailFolder As Outlook.MAPIFolder = ons.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    'emailFolder.Display()
    Dim items As Outlook.Items = emailFolder.Items
    For Each eMail As Outlook.MailItem In items
        Dim sSubject As String = eMail.Subject
        Dim sBody As String = eMail.Body
        ' etc...
        ' Code    
    Next
    oa.Quit()

    Thursday, November 30, 2017 2:16 PM
  • Hi Niels,

    You said that you want to export emails into TableView, but I can not search this control in Winform, is it a control? I do one example that exporting emails to datagridview, you can take a look.

     Dim dt As DataTable
            Try
                Dim app As Outlook.Application = New Outlook.Application()
                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, 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

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Niels Schutte Tuesday, December 5, 2017 10:16 AM
    Friday, December 1, 2017 3:10 AM
  • Thank you for all the help. I'm going to try this.
    Monday, December 4, 2017 6:43 AM
  • @kunmo bu

    What reference do you use? See picture for what references I've added:


    How do I say in VB how to use these? C#: using .........; This does not work in Visual Basic for me. If i type using "Microsoft.", Visual Basic is not showing me any options.

    What I've tried with your example code:

    Imports Outlook = Microsoft.Office.Interop.Outlook
    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As DataTable
            Try
                Dim app As Outlook.Application = New Outlook.Application()
                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, 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
    End Class
    Every instance of Outlook.Something is causing an error: "Outlook.Something not defined".

    Niels





    Monday, December 4, 2017 6:52 AM
  • Hi Niels,

    Please right-click Reference---choose Add reference--- choose COM tab

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 4, 2017 7:24 AM
  • @kunmo bu

    Oke, the errors in the code are gone. Now I'm getting the following catch exception:

    In English: "Retrieving of the COM-classfactory for the part with the CLSID {0006F03A-0000-0000-C000-0000000000046} has failed because of the following error: '80040154 Class is not registered(Exception of HRESULT: 0x80040154(REGDB_E_CLASSNOTREG)).'"

    Thanks


    Monday, December 4, 2017 7:37 AM
  • Hi,

    This error might be caused by that the outlook app already is running. If you close the Outlook app, will you receive this error?

    Regards,

    Tony


    Help each other

    Tuesday, December 5, 2017 8:41 AM
  • @Tony---- Yes, I'll still recieve the error.

    What I've tried so far:

    -Try different target platforms(x64/x86).

    -Repair MS Office.

    -Restart computer in safe mode with internet acces.

    -Run the VB application with MS Outlook application closed.

    Still not working..

    Thanks for all the help so far.

     
    Tuesday, December 5, 2017 8:57 AM
  • Same error with that code.
    Tuesday, December 5, 2017 9:09 AM
  • Check the Task Manager whether there is any outlook instance, and close it.

    Help each other

    Tuesday, December 5, 2017 9:18 AM
  • All closed. Maybe deleting and reinstalling MS Office will work? What do u think about that?
    Tuesday, December 5, 2017 9:23 AM
  • I found the error. If you dont run both under the same rights, its not working. So both on administrator or both on user than this works. Thanks for all the help.
    Tuesday, December 5, 2017 9:56 AM