I'm trying to create a program which retrieves all emails from Outlook's(2007 desktop version) Inbox and puts them into a DataGridView.
Code:
Imports Outlook = 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
When I try to Build the project I'm getting the follow error:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
UPDATE
I've changed the Compiler CPU to x86 and x64, this does not solve the error.
The problem causing the error was that both applications(VS2015 and MS Office 2007) were not running under the same rights. Open both applications with same rights(administrator or user) and the application will work. Thanks for the help!