vbaoutlookoffice-interopcom-automation

Migrating away from Office.Interop.Outlook


We currently use Microsoft.Office.Interop.Outlook to Open Outlook with a preformatted MailItem. This is so the user can change any information before the email is sent.

Searched Exchange WebServices but did not find equivalent.

I cleaned the code below as much as possible. We would like to open a message in the current version of Outlook on our users Desktops. For now, we have Exchange on prem and Office 2010 we are moving to Office 2016 and O365 in the cloud.

    Public Function OpenOutlookSendWithAttachment(ByVal Subject As String, ByVal Body As String, ByVal FileName As String) As Boolean
        Dim bSuccess As Boolean = True
        Dim OutlookApplication As Microsoft.Office.Interop.Outlook.Application
        Dim OutlookMailItem As Microsoft.Office.Interop.Outlook.MailItem

        Try
            OutlookApplication = New Microsoft.Office.Interop.Outlook.Application
            Try
                If OutlookApplication.Session.Offline Then OutlookApplication.Session.Logon("", "", True, True)
            Catch ex As Exception
            End Try
            OutlookMailItem = OutlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
            OutlookMailItem.Subject = Subject
            OutlookMailItem.Body = Body & vbNewLine
            OutlookMailItem.Attachments.Add(FileName)
            OutlookMailItem.Display(True)
        Catch ex As Exception
            bSuccess = False
        Finally
            OutlookMailItem = Nothing
            OutlookApplication = Nothing
        End Try

        Return bSuccess
    End Function

Looking for any .Net method to perform the equivalent.


Solution

  • You can use either a mailto link (which does not support attachments) or create an MSG (binary) or EML (MIME text) file: Outlook will be happy to open and show it.

    In the latter case (.EML file), don't forget to add "X-Unsent: 1" MIME header to force Outlook to treat the message as unsent and show the Send button.