user857521
user857521

Reputation:

Subscribe to Events using Late Binding

I can subscribe to Outlook events using the following in a 'Class Module' where myOlApp uses early binding.

'**Class Module - clsOutlookHandler **
Public WithEvents myOlApp As Outlook.Application

Private Sub Class_Initialize()
    On Error Resume Next
    Set myOlApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set myOlApp = CreateObject("Outlook.Application")
        Err.Clear
    End If
    On Error GoTo 0
End Sub

For this to work I need to ensure references are checked for the Microsoft Outlook XX.X Object Library.

Is it possible to subscribe to Outlook (or any Application) events using late binding?

I know the following won't work. Is there a workaround in VBA?

Public WithEvents myOlApp As Object

Upvotes: 5

Views: 1945

Answers (1)

Monty Wild
Monty Wild

Reputation: 3991

If you include a reference to a particular version of Microsoft Outlook (or any other software for that matter), and the end-user has a later version of that software, the reference will still work, provided that the vendor of that software has set up version inheritance properly, and has not made breaking changes.

However, if an end-user has an earlier version of the software (i.e. Outlook), the reference won't work, so you need to set up the reference to the earliest version of that software that you intend to support.

This way, you can avoid late binding altogether, and still allow for multiple versions of the referenced software.

You just need to be careful not to update references to older versions of your referenced software to newer versions, or you could break your workbook for users with older versions of the software.

Upvotes: 1

Related Questions