Outlook as late binding with Option Strict ON

Hugh Self Taught 81 Reputation points
2024-04-02T11:21:05.99+00:00

Hi Gurus, In an attempt to switch Option Strict to ON I've been correcting the errors & forgot that I hadn't dealt with Outlook. My current code is old VBA based late binding to avoid Office version issues. All the reading I've been able to find all requires a COM reference to Outlook and since not all machines using the application have the same Office version I'm unsure how .Net VB will be deal with that. I have O2019 but at least one of the machines has O2016 so when deployed how will that be dealt with?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,386 questions
Outlook
Outlook
A family of Microsoft email and calendar products.
2,982 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,120 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,591 Reputation points Microsoft Vendor
    2024-04-03T10:07:31.1933333+00:00

    Hi @Hugh Self Taught ,

    Attempt early binding first with New Microsoft.Office.Interop.Outlook.Application().

    If early binding fails, fall back to late binding using reflection.

    Option Strict On
    
    Public Class OutlookManager
        Private outlookApp As Object
    
        Public Sub New()
            Try
                ' Attempt early binding
                outlookApp = New Microsoft.Office.Interop.Outlook.Application()
            Catch ex As Exception
                ' Early binding failed, fall back to late binding
                Dim outlookType As Type = Type.GetTypeFromProgID("Outlook.Application")
                outlookApp = Activator.CreateInstance(outlookType)
            End Try
        End Sub
    
        ' Other methods and properties for Outlook automation
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful