Microsoft.Office.Interop.Outlook gives an unspecified error

Kurt Jensen 20 Reputation points
2024-02-19T09:39:19.79+00:00

This code gives this error: Unspecified error (0x80004005 (E_FAIL)) I have not been able to find any solution to this anywhere. Can someone here help? It is a kind of test program so I only have a listbox and a button on the form, but if I can get it to work I need this functionality in another program. Imports Microsoft.Office.Interop.Outlook Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Dim oApp As New Microsoft.Office.Interop.Outlook.Application Dim mBox As Folder = oApp.Session.Folders("Migatronic Consultant Access") 'oApp.Session.Folders("Migatronic Consultant Access") Dim Indfolder As Folder = mBox.Folders("Indbakke") Dim ProcFolder As Folder = Indfolder.Folders("Processed") MsgBox(ProcFolder.EntryID) ListBox1.Items.Clear() For Each msg As Object In ProcFolder.Items If TypeOf msg Is MailItem Then Dim wrkmsg As MailItem 'DirectCast(msg, MailItem) wrkmsg = DirectCast(msg, MailItem) If wrkmsg IsNot Nothing Then ListBox1.Items.Add(wrkmsg.Subject & " " & wrkmsg.SenderEmailAddress) End If End If Next Catch ex As System.Exception Debug.Print("An error occurred: " & ex.Message) MsgBox("An error occurred: " & ex.Message) End Try End Sub End Class Try Dim oApp As New Microsoft.Office.Interop.Outlook.Application Dim mBox As Folder = oApp.Session.Folders("Migatronic Consultant Access") Dim Indfolder As Folder = mBox.Folders("Indbakke") Dim ProcFolder As Folder = Indfolder.Folders("Processed") MsgBox(ProcFolder.EntryID) ListBox1.Items.Clear() For Each msg As Object In ProcFolder.Items If TypeOf msg Is MailItem Then Dim wrkmsg As MailItem 'DirectCast(msg, MailItem) wrkmsg = DirectCast(msg, MailItem) Dim SenderAdr As String If wrkmsg.SenderEmailType = "EX" Then SenderAdr = wrkmsg.Sender.GetExchangeUser.PrimarySmtpAddress Else SenderAdr = wrkmsg.SenderEmailAddress End If If wrkmsg IsNot Nothing Then ListBox1.Items.Add(wrkmsg.Subject & " " & SenderAdr) End If End If Next Catch ex As System.Exception Debug.Print("An error occurred: " & ex.Message) MsgBox("An error occurred: " & ex.Message) End Try End Sub End Class

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,201 Reputation points Microsoft External Staff
    2024-02-21T06:29:50.8033333+00:00

    Hi @Kurt Jensen ,

    Please ensure that the Sender object and its related properties (GetExchangeUser and PrimarySmtpAddress) are not null before accessing them.

    If wrkmsg.Sender IsNot Nothing Then
        Dim senderAddress As String = ""
        If wrkmsg.SenderEmailType = "EX" Then
            Dim exchangeUser = wrkmsg.Sender.GetExchangeUser()
            If exchangeUser IsNot Nothing Then
                senderAddress = exchangeUser.PrimarySmtpAddress
            End If
        Else
            senderAddress = wrkmsg.SenderEmailAddress
        End If
        ListBox1.Items.Add(wrkmsg.Subject & " " & senderAddress)
    End If
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Kurt Jensen 20 Reputation points
    2024-02-23T07:37:12.44+00:00

    Despite of good suggestions from those who participated, there were no solution found. I close the question.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.