View Single Post
Old Sep 17th, 2004, 02:13 PM   #11
RobDog888
Super Moderator
 
RobDog888's Avatar
 
Join Date: Apr 01
Location: LA, Calif. Raiders #1 AKA:Gangsta Yoda™
Posts: 58,875
RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)RobDog888 has a brilliant future (2000+)
Talking

Well here it is in case you can use CDO. If you did a full install of
Outlook it should be installed already. If not you can install it from
the Outlook CD. Its Microsoft Collaboration Data Objects 1.21.

For client applications, you must install Outlook in order to install
CDO. In Outlook 2000 and later versions, CDO is included with
Outlook but is not part of the default setup, nor will it install on
first run. Therefore, you must explicitly select it during a custom
setup or use the Windows Installer object library to
programmatically install CDO

I cleared out some of the names and blocked the email address
from showing so they wont get spammed just in case, but it does
fillup the listview.





VB Code:
  1. Option Explicit
  2. 'Add reference to MS CDO 1.21 Library
  3. 'Add reference to MS Office Outlook xx.x Object Library
  4. 'Add listview control (lvwGAL) 'Project > Components > MS Windows Common Controls 6.0
  5. 'Add 2 command buttons (cmdClose) and (cmdRefresh)
  6. Private moApp As Outlook.Application
  7. Private moNS As Outlook.NameSpace
  8. Private moCDO As MAPI.Session
  9. Private Const cdoPR_DISPLAY_NAME As Long = &H3001001E
  10. Private Const cdoPR_ACCOUNT As Long = &H3A00001E
  11. Private Const cdoPR_GIVEN_NAME As Long = &H3A06001E
  12. Private Const cdoPR_SURNAME As Long = &H3A11001E
  13. Private Const cdoPR_EMAIL As Long = &H39FE001E
  14. Private Sub cmdClose_Click()
  15.     Unload Me
  16. End Sub
  17. Private Sub cmdRefresh_Click()
  18.     GetAddressCDO
  19. End Sub
  20. Private Sub Form_Load()
  21.    
  22.     On Error GoTo No_Bugs
  23.    
  24.     'INITIALIZE OOM (JUST SO CDO CAN LOGON TO INSTANCE)
  25.     Set moApp = GetObject(, "Outlook.Application")
  26.     If TypeName(moApp) = "Nothing" Then
  27.         Set moApp = New Outlook.Application
  28.     End If
  29.     Set moNS = moApp.GetNamespace("MAPI")
  30.     'INITIALIZE CDO
  31.     Set moCDO = CreateObject("MAPI.Session")
  32.     moCDO.Logon "", "", False, False
  33.     'INITIALIZE LISTVIEW
  34.     With lvwGAL
  35.         .FullRowSelect = True
  36.         .LabelEdit = lvwManual
  37.         .MultiSelect = False
  38.         .View = lvwReport
  39.         .ColumnHeaders.Add , , "", 0
  40.         .ColumnHeaders.Add , , "Display Name", (.Width / 5) - 70
  41.         .ColumnHeaders.Add , , "Last Name", (.Width / 5) - 70
  42.         .ColumnHeaders.Add , , "First Name", (.Width / 5) - 70
  43.         .ColumnHeaders.Add , , "Alias", (.Width / 5) - 70
  44.         .ColumnHeaders.Add , , "E-Mail", (.Width / 5) - 70
  45.         .Sorted = True
  46.         .SortOrder = lvwAscending
  47.         .SortKey = 1
  48.     End With
  49.     'POPULATE LISTVIEW
  50.     GetAddressCDO
  51.     Exit Sub
  52.    
  53. No_Bugs:
  54.     If Err.Number = 429 Then
  55.         Resume Next
  56.     Else
  57.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
  58.     End If
  59. End Sub
  60. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  61.     moCDO.Logoff
  62.     Set moCDO = Nothing
  63.     Set moNS = Nothing
  64.     Set moApp = Nothing
  65. End Sub
  66. Private Sub lvwGAL_DblClick()
  67.     On Error Resume Next
  68.     Dim oDetails As Object
  69.     Set oDetails = moCDO.GetAddressEntry(lvwGAL.SelectedItem)
  70.     oDetails.Details
  71.     Set oDetails = Nothing
  72. End Sub
  73. Private Sub GetAddressCDO()
  74.     On Error GoTo No_Bugs
  75.    
  76.     Dim oAEntries As Object
  77.     Dim oAEntry As Object
  78.     Dim oFields As Object
  79.     Dim lvItm As ListItem
  80.     Dim i As Integer
  81.     Dim ii As Integer
  82.     Dim iii As Integer
  83.     Dim vDetails As Variant
  84.     Dim lRet As Long
  85.    
  86.     lvwGAL.ListItems.Clear
  87.     Set oAEntries = moCDO.AddressLists.Item("Global Address List").AddressEntries
  88.     For i = 1 To oAEntries.Count
  89.         Set oAEntry = oAEntries.Item(i)
  90.         Set lvItm = lvwGAL.ListItems.Add(, , oAEntry.ID)
  91.         'ITERATE THROUGH ALL FIELDS OF THIS ADDRESS ENTRY
  92.         For ii = 1 To oAEntry.Fields.Count
  93.             Set oFields = oAEntry.Fields(ii)
  94.             vDetails = oFields.Value
  95.             On Error Resume Next
  96.             lRet = UBound(vDetails)
  97.             If Err.Number = 0 Then
  98.                 'ITERATE THROUGH ALL VALUES FOR THIS ARRAY
  99.                 For iii = 0 To UBound(vDetails)
  100.                     'Debug.Print "Field ID:  " & oFields.ID & "; Field Array #" & iii & ";Value:  " & vDetails(iii)
  101.                 Next
  102.             Else
  103.                 'ADD SINGLE VALUES TO THE LISTVIEW
  104.                 If oFields.ID = cdoPR_DISPLAY_NAME Then
  105.                     lvItm.SubItems(1) = vDetails
  106.                 ElseIf oFields.ID = cdoPR_SURNAME Then
  107.                     lvItm.SubItems(2) = vDetails
  108.                 ElseIf oFields.ID = cdoPR_GIVEN_NAME Then
  109.                     lvItm.SubItems(3) = vDetails
  110.                 ElseIf oFields.ID = cdoPR_ACCOUNT Then
  111.                     lvItm.SubItems(4) = vDetails
  112.                 ElseIf oFields.ID = cdoPR_EMAIL Then
  113.                     lvItm.SubItems(5) = vDetails
  114.                 End If
  115.                 'Debug.Print "Field ID:  " & oFields.ID & "; Value  " & vDetails
  116.             End If
  117.         Next
  118.         Set lvItm = Nothing
  119.     Next
  120.     Set oAEntries = Nothing
  121.     Set oAEntry = Nothing
  122.     Set oFields = Nothing
  123.     Exit Sub
  124.    
  125. No_Bugs:
  126.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
  127. End Sub


VB/Outlook Guru!!!
Attached Images
 
__________________
VB/Office Guru™ (AKA: Gangsta Yoda®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.

Microsoft MVP 2006, 2007, 2008, 2009, 2010
Office Development FAQ (VBA, VB 6, VB.NET, C#)
Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007
RobDog888 is offline   Reply With Quote