hatman
Well-known Member
- Joined
- Apr 8, 2005
- Messages
- 2,664
My code works well for computer with and without Outlook INstalled. Provided an Outlook Session is already running, then this whole thing works fine, however, if not, then the SetForeGroundWindow is not able to bring the dialog to the foreground. My users are the brightest bulbs, and a blinking Outlook icon in the Taskbar is not adequate to alert them that a dialog is waiting for them. As soon as teh .Display method is invoked, XL waits for the Dialog to be dismissed by the user. Until that point, there is no visible window for the SetForeGroundWindow function to grab hold of...
Please Help.
Please Help.
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Public Function ShowOnlyContacts(Optional ByVal vstrContacts As String = "") As String
Const c_strOutlook_Class As String = "rctrl_renwnd32"
#If Development Then
Dim olApp As Outlook.Application
Dim oAL As Outlook.AddressList
Dim oDialog As Outlook.SelectNamesDialog
Dim oMsg As Outlook.MailItem
#Else
Dim olApp As Object
Dim oAL As Object
Dim oDialog As Object
Dim oMsg As Object
#End If
Dim strRecipients As String
Dim arrRecipient() As String
Dim intIndex As Integer
Dim lngHWND As Long
Dim blnCreated_Outlook As Boolean
DoEvents
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
#If Development Then
Set olApp = VBA.CreateObject("Outlook.Application")
#Else
Set olApp = New Outlook.Application
#End If
blnCreated_Outlook = True
End If
On Error GoTo 0
If olApp Is Nothing Then
' MsgBox "Please install Microsoft Outlook on this machine and try again.", vbOKOnly +vbCritical , "OOPS"
Exit Function
End If
lngHWND = FindWindow(c_strOutlook_Class, vbNullString)
SetForegroundWindow lngHWND
Set oMsg = olApp.CreateItem(olMailItem)
Set oDialog = olApp.Session.GetSelectNamesDialog
arrRecipient = Split(vstrContacts, ";")
For intIndex = 0 To UBound(arrRecipient)
oMsg.Recipients.Add arrRecipient(intIndex)
Next intIndex
Set oAL = olApp.GetNamespace("MAPI").AddressLists("Global Address List")
With oDialog
.InitialAddressList = oAL
.ShowOnlyInitialAddressList = True
.Recipients = oMsg.Recipients
.NumberOfRecipientSelectors = olShowTo
.Display
DoEvents
End With
strRecipients = ""
For intIndex = 1 To oMsg.Recipients.Count
strRecipients = strRecipients & "; " & oMsg.Recipients(intIndex)
Next
ShowOnlyContacts = Mid(strRecipients, 2)
SetForegroundWindow Application.hwnd
Set oDialog = Nothing
Set oAL = Nothing
Set oMsg = Nothing
If blnCreated_Outlook Then
olApp.Quit
End If
Set olApp = Nothing
End Function