- Subject: Why Does GetObject Create New Instances of Outlook 2007+
- Author: rob
- Date: 07 Jun 2010
- References:
I'm using GetObject to automate an existing instance of Outlook using VBA from
Excel to open the address book and allow the user to select a group of
recipients.
When I do this with Outlook 2007 or 2010 an new Outlook window is opened each
time the code is run. If I quit the instance of Outlook I'm holding then
Outlook
is shut down completely, regardless of how many windows are showing in the
TaskBar.
Repro code is shown below. How can I get this to not open a new Outlook window
each time it's run? Thanks!
Sub ShowAddressList()
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objDialog As Outlook.SelectNamesDialog
Dim objRecipient As Outlook.Recipient
Set objOutlook = GetObject(, "Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objDialog = objNameSpace.GetSelectNamesDialog
objDialog.Display
For Each objRecipient In objDialog.Recipients
Debug.Print objRecipient.Name
Next objRecipient
Set objDialog = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Sub