I am working on a task where I am reading mails and compare the subject and Body with my existing Mapping file.
In few emails there are Outlook Items attached and I want to extract data from those .msg attachments.
I have written this code, it is working in some cases but sometimes it is giving me an error: “Exception has been thrown by Target of Invocation”
Please find the below code:
Dim Outlook As _Application = New Microsoft.Office.Interop.Outlook.Application()
Dim item As MailItem = CType(Outlook.CreateItemFromTemplate(in_MsgFile, Type.Missing), MailItem)
Can anybody help me with this issue.
Code for .msg which is in html format.
Dim Outlook As _Application = New Microsoft.Office.Interop.Outlook.Application()
Dim item As MailItem = CType(Outlook.CreateItemFromTemplate(in_MsgFile, Type.Missing), MailItem)
out_Subject = item.Subject
If item.BodyFormat = OlBodyFormat.olFormatHTML Then
out_Body = item.HTMLBody
Else
out_Body = item.Body
End If
out_To = item.To
This code is for .msg file if it not plain text but html. This is also not working and not giving any error by which I can get the root cause of the Issue.
Try
' Initialize the Outlook Application
Dim Outlook As Microsoft.Office.Interop.Outlook._Application = New Microsoft.Office.Interop.Outlook.Application()
' Create the MailItem from the .msg file
Dim item As Microsoft.Office.Interop.Outlook.MailItem = CType(Outlook.CreateItemFromTemplate(in_MsgFile, Type.Missing), Microsoft.Office.Interop.Outlook.MailItem)
' Extract the Subject
out_Subject = item.Subject
' Check if the body is in HTML format
If item.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML Then
out_Body = item.HTMLBody
Else
out_Body = item.Body
End If
' Extract the recipient(s)
out_To = item.To
Catch ex As Exception
' Log the exception details for further investigation
Msgbox "Error processing .msg file: " & in_MsgFile & " - " & ex.Message & " - " & ex.InnerException?.Message
' Optionally, rethrow the exception if you want it to bubble up
Throw
End Try
I have tried it but this Exception is coming.
No compiled code to run
error BC30800: Method arguments must be endosed in parentheses. At
line 22
error BC30561: ‘Exception’ is ambiguous, imported from the namespaces or types System, Microsoft Office.Interop Outlook. At line 20.
I have updated the code and make it this.
Try
’ Initialize the Outlook Application
Dim Outlook As Microsoft.Office.Interop.Outlook._Application = New Microsoft.Office.Interop.Outlook.Application()
' Create the MailItem from the .msg file
Dim item As Microsoft.Office.Interop.Outlook.MailItem = CType(Outlook.CreateItemFromTemplate(in_MsgFile, Type.Missing), Microsoft.Office.Interop.Outlook.MailItem)
' Extract the Subject
out_Subject = item.Subject
' Check if the body is in HTML format
If item.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML Then
out_Body = item.HTMLBody
Else
out_Body = item.Body
End If
' Extract the recipient(s)
out_To = item.To
Catch ex As System.Exception
’ Log the exception details for further investigation
MsgBox("Error processing .msg file: " & in_MsgFile & " - " & ex.Message & If(ex.InnerException IsNot Nothing, " - " & ex.InnerException.Message, “”))
’ Optionally, rethrow the exception if you want it to bubble up
Throw
End Try
Now It is not giving any exception on invoke code, but it got stuck and not moving forward with next activity.