Code not giving expected results

Devon Nullman 21 Reputation points
2024-02-05T01:01:15.52+00:00
Code:
Imports Microsoft.Office.Interop.Outlook
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oApp As New Application
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("MAPI")
        Dim RootFolder As MAPIFolder = oNS.DefaultStore.GetRootFolder

        For Each MFolder As Microsoft.Office.Interop.Outlook.Folder In RootFolder.Folders
            Try
                If MFolder.DefaultItemType = OlItemType.olMailItem Then
                    Dim MBItems As Items = MFolder.Items
                    For Each EMailitem As MailItem In MBItems
                        Dim Atchmnts As Attachments = EMailitem.Attachments
                        If Atchmnts.Count > 0 Then
                            TextBox1.AppendText("Folder: " & MFolder.Name & vbNewLine)
                            TextBox1.AppendText("From: " & EMailitem.SenderName & vbNewLine)
                            TextBox1.AppendText("Subj: " & EMailitem.Subject & vbNewLine)
                            TextBox1.AppendText(Atchmnts.Count.ToString & " Attachment(s):" & vbNewLine)
                            For Each a As Microsoft.Office.Interop.Outlook.Attachment In Atchmnts
                                TextBox1.AppendText(vbTab & a.FileName & " - " & a.Size.ToString & " bytes" & vbNewLine)
                            Next
                            TextBox1.AppendText(vbNewLine)
                        End If
                    Next
                End If
            Catch
                TextBox1.AppendText("Error Processing " & vbNewLine)
            End Try
        Next
        TextBox1.AppendText("Finished")
        oApp.Quit()
        oApp = Nothing
        oNS = Nothing
    End Sub
End Class

One result:
Folder: Inbox
From: Maria xxxxxxxxxxxxx
Subj: RE: Sewer Drawing
3 Attachment(s):
image001.png - 6561 bytes (embedded logo, not a real attachment)
image002
38883.tif - 1052020 bytes (actual attached file)


Microsoft 365 and Office | Development | Other
Outlook | Windows | Classic Outlook for Windows | For business
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Developer technologies | VB
{count} votes

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.