Option strict on disallows late binding, VB in Invoke code


Hiya, I am writing a process to read .msg files with an Invoke code activity and cannot manage to solve this issues. Would very much appreciate any feedback. Cheers and thank you for your help :slight_smile:

Give a try at the following

Define variables along with the datatype

Dim myVarName As theDataType

then instantiate / assign the defined variable

In line 1 we can see that this is not implemented as described and could look like this:
Dim outlookApp As …
outlookApp = new …

@idoia.fernandez,

Basically the error states - VB.NET requires explicit typing when Option Strict is enabled. In your code, you’re using late binding when accessing properties like email.Subject , email.Body , and email.SenderName without declaring email with a specific type.

Try this code.

Dim outlookApp As New Microsoft.Office.Interop.Outlook.Application
Dim msgFilePath As String = EmailFilename

' Explicitly declare the type of email
Dim email As Microsoft.Office.Interop.Outlook.MailItem = TryCast(outlookApp.Session.OpenSharedItem(msgFilePath), Microsoft.Office.Interop.Outlook.MailItem)

If email IsNot Nothing Then
    Dim Subject As String = email.Subject
    Dim Body As String = email.Body
    Dim Sender As String = email.SenderName
    ' You can now use Subject, Body, and Sender as needed
Else
    ' Handle the case where the item is not a MailItem
    Throw New InvalidCastException("The shared item is not a MailItem.")
End If

LLM helped me to rewrite this code

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.