u795696
asked on
VB6 Open Outlook 2007 Addressbook
Hello,
i'm developing an application that will allow users to send email to selected people.
They need to be able to select the users from the Outlook Global Addresslist.
I am able to loop through the existing users in the domain with the following code:
[code]
Private Sub cmdEmailAdd_Click()
Dim oAdrLst As Outlook.AddressList
Dim oAdrEnt As Outlook.AddressEntries
Dim oadrentry As Outlook.AddressEntry
Dim objApp As New Outlook.Application
Set oAdrLst = objApp.GetNamespace("MAPI" ).AddressL ists("All Users") 'All Users on Domain
Set oAdrEnt = oAdrLst.AddressEntries
For Each oadrentry In oAdrEnt
Debug.Print oadrentry.Name
Next
Set oadrentry = Nothing
Set oAdrEnt = Nothing
Set oAdrLst = Nothing
Set objApp = Nothing
End Sub
[/code]
In the debu window all the names of the users in the All Users list are printed so that works OK and I am able to connect to the Outlook Addressbook.
Bu now I would like the Addressbook to appear and let the user select one or more adresses from the list and store them in a variable.
I do not know how to display the addressbook and I hope that someone here knows the answer.
i'm developing an application that will allow users to send email to selected people.
They need to be able to select the users from the Outlook Global Addresslist.
I am able to loop through the existing users in the domain with the following code:
[code]
Private Sub cmdEmailAdd_Click()
Dim oAdrLst As Outlook.AddressList
Dim oAdrEnt As Outlook.AddressEntries
Dim oadrentry As Outlook.AddressEntry
Dim objApp As New Outlook.Application
Set oAdrLst = objApp.GetNamespace("MAPI"
Set oAdrEnt = oAdrLst.AddressEntries
For Each oadrentry In oAdrEnt
Debug.Print oadrentry.Name
Next
Set oadrentry = Nothing
Set oAdrEnt = Nothing
Set oAdrLst = Nothing
Set objApp = Nothing
End Sub
[/code]
In the debu window all the names of the users in the All Users list are printed so that works OK and I am able to connect to the Outlook Addressbook.
Bu now I would like the Addressbook to appear and let the user select one or more adresses from the list and store them in a variable.
I do not know how to display the addressbook and I hope that someone here knows the answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've been trying to fix the problem above and by adding the line:
oApp.logon ,,FALSE, FALSE
before setting the oDialog, the adressbook is displayed and I am able to select users from the adressbook.
But it still does not really give me the information I need because in the recipients-property of oDialog all the selected users are stored and I can get various properties of these users but not the Email-adress.
It looks like this is stored in something like contact-information but so far I've not been able to read that data.
Any help would be appreciated :)
The code so far:
Dim oApp As New Outlook.Application
Dim oDialog As Outlook.SelectNamesDialog
Dim oRecipient As Outlook.Recipient
oApp.Session.Logon "", "", False, False
Set oDialog = oApp.Session.GetSelectName sDialog
oDialog.AllowMultipleSelec tion = True
oDialog.Display
For Each oRecipient In oDialog.Recipients
Debug.Print oRecipient.Name
Next
Set oDialog = Nothing
Set oApp = Nothing
oApp.logon ,,FALSE, FALSE
before setting the oDialog, the adressbook is displayed and I am able to select users from the adressbook.
But it still does not really give me the information I need because in the recipients-property of oDialog all the selected users are stored and I can get various properties of these users but not the Email-adress.
It looks like this is stored in something like contact-information but so far I've not been able to read that data.
Any help would be appreciated :)
The code so far:
Dim oApp As New Outlook.Application
Dim oDialog As Outlook.SelectNamesDialog
Dim oRecipient As Outlook.Recipient
oApp.Session.Logon "", "", False, False
Set oDialog = oApp.Session.GetSelectName
oDialog.AllowMultipleSelec
oDialog.Display
For Each oRecipient In oDialog.Recipients
Debug.Print oRecipient.Name
Next
Set oDialog = Nothing
Set oApp = Nothing
Does the address property work? e.g.
For Each oRecipient In oDialog.Recipients
Debug.Print oRecipient.Address
Next
If this doesn't work, then I'm guessing its a security issue, and Outlook is trying to display the message "a program is trying to access your email addresses" but you just can't see it. Try making Outlook visible while your code is running and see if the message shows up.
Unfortunately all the options to get around this in vb6 are pretty tricky. Apparently if Outlook 2007 thinks that the PC it is running on is "up to date with antivirus protection" then it will not bring up the prompts (I have no idea how it determines this). See the section "Version-specific considerations" on
http://www.outlookcode.com/article.aspx?ID=52
ASKER
I've been working on this problem yesterday and came up with a sort of workaround to get the email-adress of the selected users.
The code is like this:
On Error GoTo handler
Dim oApp As New Outlook.Application
Dim oDialog As Outlook.SelectNamesDialog
Dim oRecipient As Outlook.Recipient
Dim ocontact As Outlook.ContactItem
Dim oExch As Outlook.ExchangeUser
oApp.Session.Logon "", "", False, False
Set oDialog = oApp.Session.GetSelectName sDialog
oDialog.AllowMultipleSelec tion = True
oDialog.Display
If oDialog.Recipients.Count > 0 Then
For Each oRecipient In oDialog.Recipients
Debug.Print oRecipient.Name
Set oExch = oRecipient.AddressEntry.Ge tExchangeU ser
List1.AddItem oExch.Alias & strDomain
Set oExch = Nothing
Next
End If
Set oDialog = Nothing
oApp.Session.Logoff
Set oApp = Nothing
Exit Sub
handler:
MsgBox "cmdEmailAdd_Click()" & vbCrLf & Err.Number & vbCrLf & Err.Description
You need to add the line oApp.logon to start an Outlook-session, Outlook doesn't have to be running.
With oDialog.show you will open up the address-book and the user can select recipients.
Then I got the Alias property from the selected user by getting the Exchangeuser command.
I was not able to get the complete email-adress but our company email-addresses are build-up from the alias of an exchange-user followed by the domain-name.
This works for me.
When I start the process of selecting users from my addressbook, Outlook prompts me that the programs wants to access the addressbook and I can allow or deny this action.
Not really a complete solution for my question by you Snowberry, but I'll reward you with the points because it put me on the right track.
Thanks!
The code is like this:
On Error GoTo handler
Dim oApp As New Outlook.Application
Dim oDialog As Outlook.SelectNamesDialog
Dim oRecipient As Outlook.Recipient
Dim ocontact As Outlook.ContactItem
Dim oExch As Outlook.ExchangeUser
oApp.Session.Logon "", "", False, False
Set oDialog = oApp.Session.GetSelectName
oDialog.AllowMultipleSelec
oDialog.Display
If oDialog.Recipients.Count > 0 Then
For Each oRecipient In oDialog.Recipients
Debug.Print oRecipient.Name
Set oExch = oRecipient.AddressEntry.Ge
List1.AddItem oExch.Alias & strDomain
Set oExch = Nothing
Next
End If
Set oDialog = Nothing
oApp.Session.Logoff
Set oApp = Nothing
Exit Sub
handler:
MsgBox "cmdEmailAdd_Click()" & vbCrLf & Err.Number & vbCrLf & Err.Description
You need to add the line oApp.logon to start an Outlook-session, Outlook doesn't have to be running.
With oDialog.show you will open up the address-book and the user can select recipients.
Then I got the Alias property from the selected user by getting the Exchangeuser command.
I was not able to get the complete email-adress but our company email-addresses are build-up from the alias of an exchange-user followed by the domain-name.
This works for me.
When I start the process of selecting users from my addressbook, Outlook prompts me that the programs wants to access the addressbook and I can allow or deny this action.
Not really a complete solution for my question by you Snowberry, but I'll reward you with the points because it put me on the right track.
Thanks!
ASKER
The given solution was not complete and needed some extra programming but it put me on the right track to trigger the problem and come to a workable solution.
ASKER
thanks for the link. I looked at it and tried it but it gives me an Error 287 "Application-defined or object-defined error"on the line:
set oDialog = oApp.Session.GetSelectedNa
I've used the code below and created a reference to MS Outlook 12.0 Object Library.
[code]
Dim oApp As New Outlook.Application
Dim oMsg As Outlook.MailItem
Set oMsg = oApp.CreateItem(olMailItem
Dim oDialog As Outlook.SelectNamesDialog
Set oDialog = oApp.Session.GetSelectName
With oDialog
.InitialAddressList = _
oApp.Session.GetGlobalAddr
.Recipients = oMsg.Recipients
If .Display Then
'Recipients Resolved
oMsg.Subject = "Hello"
oMsg.Send
End If
End With
[/code]
I have no clue why this doesn't work.
Hope somebody can help me out.