Make sure to import the namespaces
Microsoft.Office.Interop.OutlookSystem.IOSystem.Runtime.InteropServices
First use assign statement to get the files
msgFiles = Directory.GetFiles("C:\SourceFolder", "*.msg") were msgFiles is array of Strings
index = 0
I just tried with this invoke code (LLM Generated with some changes) and it works, pass the msgFiles and index as arguments
Dim outlookApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application
Dim msg As Microsoft.Office.Interop.Outlook.MailItem = DirectCast(outlookApp.Session.OpenSharedItem(msgFiles(index)), Microsoft.Office.Interop.Outlook.MailItem)
' Save as EML format (Use 3 instead of olRFC822)
Dim emlPath As String = "C:\Users\ eml folder path" & System.IO.Path.GetFileNameWithoutExtension(msgFiles(index)) & ".eml"
msg.SaveAs(emlPath, 3) ' 3 corresponds to olRFC822
' Save Attachments
If msg.Attachments.Count > 0 Then
Dim attachmentPath As String = "C:\Attachment path\"
For i As Integer = 1 To msg.Attachments.Count
msg.Attachments.Item(i).SaveAsFile(System.IO.Path.Combine(attachmentPath, msg.Attachments.Item(i).FileName))
Next
End If
msg.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)
outlookApp.Quit()
Please mark it as a solution if it helps !
Happy Automation