Bring Outlook 'Select Names Dialog' to front

sydc

Registered User.
Local time
Today, 19:45
Joined
May 28, 2010
Messages
10
Bring Outlook 'Select Names Dialog' to front when selecting from Access 2010 form

Hi,
I have written the following code to open the Outlook 2013 'Select Names Dialog' with a command button called 'btnTo' from an Access 2010 form, this code works fine to display the dialogue box but it is usually hidden and the user has to 'click' the icon on the task bar to show the dialogue box. I know that there is an API that will bring the dialogue box to the front but I have no idea on how to code the API to do this.
Please can anyone help?

Code:
Private Sub btnTo_Click()
        On Error GoTo Err_btnTo_Click
 
       Dim olkApp As Outlook.Application
       Dim olkSes As Outlook.NameSpace
       Dim olkSND As Outlook.SelectNamesDialog
       Dim olkRecipient As Outlook.Recipient
       Dim olkTo As String
       Dim x As Integer
 
       Set olkApp = CreateObject("Outlook.Application")
       Set olkSes = olkApp.Session
       Set olkSND = olkSes.GetSelectNamesDialog
 
       With olkSND
 
            .AllowMultipleSelection = True
 
            If .Display Then
            For Each olkRecipient In .Recipients
            x = x + 1
            olkTo = olkTo & .Recipients(x) & "; "
            Next
            End If
 
            End With
 
          Me!txbTo.Value = olkTo
 
        Set olkSND = Nothing
        Set olkSes = Nothing
        Set olkApp = Nothing
 
Exit_btnTo_Click:
    Exit Sub
 
Err_btnTo_Click:
    MsgBox Err.Description
    Resume Exit_btnTo_Click
 
End Sub

Any help would be much appreciated.

Regards,
Syd.
 
Last edited:
Declare these two APIs in a module:
Code:
Public Declare Function FindWindow Lib "User32" _
                        Alias "FindWindowA" ( _
                        ByVal ClassName As String, _
                        ByVal Title As String) As Long
                        
Public Declare Function SetForegroundWindow Lib "user32.dll" _
                        (ByVal hwnd As Long) As Long
Include these lines in your code. The highlighted part is your code:
Code:
[COLOR="Blue"]Dim x As Integer[/COLOR]
Dim outlkHWnd As Long
Dim blDisplay As Boolean

[COLOR="blue"]...[/COLOR]

[COLOR="Blue"]Set olkSND = olkSes.GetSelectNamesDialog[/COLOR]

outlkHWnd = FindWindow("rctrl_renwnd32", vbNullString)

[COLOR="blue"]With olkSND

        .AllowMultipleSelection = True[/COLOR]
    
        blDisplay = .Display
        SetForegroundWindow outlkHWnd
        
        If blDisplay Then
It works when Outlook is closed. So if Outlook is already opened you will have to use GetObject() instead of CreatObject() and you'll need to replace vbNullString with the Caption of the Application.Explorer object. Not tested this concept but I believe that's how it should be.
 
@vbaInet

CreateObject is clever enough to know that only one instance of Outlook is allowed, and so it creates the object if Outlook is closed or gets the existing one if Outlook is open.
 
Not really, they are two different things.

It wouldn't selectively pick an instance without an explicit say-so. CreateObject creates a new instance whereas GetObject gets a reference to the current instance. You can run a test and look at the Task Manager.

It's best to be explicit in your calls anyway.
 
Or are you talking about the fact that Outlook is a single instance object? From that viewpoint then I see where you're coming from. But it's not CreateObject() that does the deciding, it's the way the Class was defined.

@sydc: It seems if Outlook is already running all you need to do is bring the Outlook window to the front, no APIs needed. ActiveWindow.Activate should do it.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom