none
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 1:21 PM

Answers

  • Hi Niels,

    I suggest you make a test with code below:

    Imports System.Reflection
    Imports System.Runtime.InteropServices
    Imports Microsoft.Office.Interop
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        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
    
    End Class
    

    Best Regards,

    Tao Zhou


    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 9:18 AM
  • Hi Niels,

    Do you run Outlook and your VB.NET application under the same authority?

    In other words, if you run the Outlook under administrator, your VB.net application should run administrator too.

    I got the similar error while I run VB.net under administror, and Outlook not.

    Best Regards,

    Tao Zhou


    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.

    Tuesday, December 5, 2017 9:30 AM

All replies

  • Hello Niels,

    Looks like you need to automate Outlook to get the required information for the table view. Take a look at the C# app automates Outlook (CSAutomateOutlook) sample application which explains the required steps in depth.

    Also take a look at the Writing .NET Code for Outlook: VB.NET, C# samples page.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, November 30, 2017 7:52 PM
  • Hi Niels,

    I suggest you make a test with code below:

    Imports System.Reflection
    Imports System.Runtime.InteropServices
    Imports Microsoft.Office.Interop
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        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
    
    End Class
    

    Best Regards,

    Tao Zhou


    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 9:18 AM
  • Hello,

    Thanks for the code. As well as my other post, im getting an error. Please view the following post to see the error and what I've tried to solve this: 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/304aceff-110d-4a6e-b6ee-2aa5d78d2cf9/export-emails-from-outlook-email-into-windows-form-application?forum=vbgeneral#09f0f17e-9815-4fcb-8c70-5b6f25f57ef7

    Thanks!

    Tuesday, December 5, 2017 9:13 AM
  • Hi Niels,

    Do you run Outlook and your VB.NET application under the same authority?

    In other words, if you run the Outlook under administrator, your VB.net application should run administrator too.

    I got the similar error while I run VB.net under administror, and Outlook not.

    Best Regards,

    Tao Zhou


    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.

    Tuesday, December 5, 2017 9:30 AM
  • Wow! This solved the error.. Only error im getting now is:

    In English: "The open member Sender for type MailItem has not been found".

    Thanks

    Tuesday, December 5, 2017 9:47 AM
  • Hi Niels,

    I am glad your original issue has been resolved, and I would suggest you mark the solution as answer to close this thread.

    For this new issue, I would suggest you post a new thread. 

    Best Regards,

    Tao Zhou


    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.


    Tuesday, December 5, 2017 10:03 AM
  • Oke, I've done that. Posting new thread now. Hope I see you there:). Thanks.
    Tuesday, December 5, 2017 10:17 AM