locked
Converting Early Binding Code to Late Binding Code RRS feed

  • Question

  • Hi,

    I have a small VBA code which runs in MS Word to select recipient from outlook using GetSelectNamesDialog. It runs smoothly however the only issue is that the code is in early bound. Is there a way to late bind this below code.

    Thanks,
    Arun N

    Sub ResolveBySelection()
        
        Dim OutApp As Outlook.Application
        Set OutApp = New Outlook.Application
      
        Dim oDialog As SelectNamesDialog
        Set oDialog = OutApp.Session.GetSelectNamesDialog
      
        With oDialog
            .AllowMultipleSelection = False
            .NumberOfRecipientSelectors = olShowNone
            If .Display Then
                MsgBox oDialog.Recipients(1)
            End If
        End With
    
    End Sub

    Tuesday, April 18, 2017 3:21 AM

Answers

  • If you are working with the Outlook object from Word, then you should be aware that Outlook doesn't behave correctly when a new Outlook object is created and this macro will fail - and does so even with the corrections made by Hans. (It is OK if you get the current Outlook object).

    To ensure that Outlook is opened correctly, I would urge you to investigate the function by Ben Clothier - http://www.rondebruin.nl/win/s1/outlook/openclose.htm and use that to Get/Create the Outlook application.

    The code would then look like

    Sub ResolveBySelection()
        Dim OutApp As Object
        Set OutApp = OutlookApp() 'Call Ben Clothier's function to open Outlook
        Dim oDialog As Object
        Set oDialog = OutApp.Session.GetSelectNamesDialog

        With oDialog
            .AllowMultipleSelection = False
            .NumberOfRecipientSelectors = 0 ' olShowNone
            If .Display Then
                MsgBox oDialog.Recipients.Item(1).Name
            End If
        End With
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, April 18, 2017 6:02 AM
  • For example:

    Sub ResolveBySelection()
        Dim OutApp As Object
        Set OutApp = CreateObject("Outlook.Application")
    
        Dim oDialog As Object
        Set oDialog = OutApp.Session.GetSelectNamesDialog
    
        With oDialog
            .AllowMultipleSelection = False
            .NumberOfRecipientSelectors = 0 ' olShowNone
            If .Display Then
                MsgBox oDialog.Recipients.Item(1).Name
            End If
        End With
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Tuesday, April 18, 2017 5:22 AM

All replies

  • For example:

    Sub ResolveBySelection()
        Dim OutApp As Object
        Set OutApp = CreateObject("Outlook.Application")
    
        Dim oDialog As Object
        Set oDialog = OutApp.Session.GetSelectNamesDialog
    
        With oDialog
            .AllowMultipleSelection = False
            .NumberOfRecipientSelectors = 0 ' olShowNone
            If .Display Then
                MsgBox oDialog.Recipients.Item(1).Name
            End If
        End With
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Tuesday, April 18, 2017 5:22 AM
  • If you are working with the Outlook object from Word, then you should be aware that Outlook doesn't behave correctly when a new Outlook object is created and this macro will fail - and does so even with the corrections made by Hans. (It is OK if you get the current Outlook object).

    To ensure that Outlook is opened correctly, I would urge you to investigate the function by Ben Clothier - http://www.rondebruin.nl/win/s1/outlook/openclose.htm and use that to Get/Create the Outlook application.

    The code would then look like

    Sub ResolveBySelection()
        Dim OutApp As Object
        Set OutApp = OutlookApp() 'Call Ben Clothier's function to open Outlook
        Dim oDialog As Object
        Set oDialog = OutApp.Session.GetSelectNamesDialog

        With oDialog
            .AllowMultipleSelection = False
            .NumberOfRecipientSelectors = 0 ' olShowNone
            If .Display Then
                MsgBox oDialog.Recipients.Item(1).Name
            End If
        End With
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, April 18, 2017 6:02 AM