Mit VB.NET auf öffentliche/geteilte Kontakte in Outlook zugreifen

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Hanseat.

    Mit VB.NET auf öffentliche/geteilte Kontakte in Outlook zugreifen

    Hallo zusammen,

    ich brauche bitte einmal kurz Hilfe. Ich bin sicher, es ist nur ein ganz kleines Ding, aber ich komme nicht drauf und finde auch nix passendes..

    Für unser kleines Start-Up nutzen wir einen Hosted-Exchange um mit Outlook (2016) auf Emails und Kontakte gemeinsam zugreifen zu können. Das klappt auch alles wunderbar. Jetzt möchte ich das geteilte, gemeinsame Adressbuch per VB ansprechen, um Kontakte auszulesen und zu bearbeiten.

    Das klappt auf vielerlei Weise mit meinen privaten Kontakten, die ich unterschiedlich ansprechen kann, aber es gelingt mir nicht, auf unsere gemeinsamen Kontakte "Firmenkontakte" zugreifen zu können. Was muss ich ändern, um statt den Default-Kontakten mit Kontakten meiner Wahl arbeiten zu können?

    Hier ist ein Beispiel (von vielen), wie ich auch meine Privatkontakte zugreifen kann:

    Aufruf mit:


    VB.NET-Quellcode

    1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    2. Call AccessContacts("TestName")
    3. end sub


    Code:

    VB.NET-Quellcode

    1. Private Sub AccessContacts(ByVal findLastName As String)
    2. Dim oApp As Outlook.Application = New Outlook.Application()
    3. Dim folderContacts As Outlook.MAPIFolder = oApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    4. Dim searchFolder As Outlook.Items = folderContacts.Items
    5. Dim counter As Integer = 0
    6. For Each foundContact As Outlook.ContactItem In searchFolder
    7. If foundContact.LastName.Contains(findLastName) Then
    8. foundContact.Display(False)
    9. counter = counter + 1
    10. End If
    11. Next
    12. MsgBox("You have " & counter & " contacts with last names that contain " & findLastName & ".")
    13. End Sub
    14. Private Function GetDefaultAddressBook() As Outlook.MAPIFolder
    15. Dim oApp As Outlook.Application = New Outlook.Application()
    16. Dim ns As Outlook._NameSpace = oApp.GetNamespace("MAPI")
    17. If ns IsNot Nothing Then
    18. Try
    19. Return ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    20. Finally
    21. ns = Nothing
    22. End Try
    23. End If
    24. Return Nothing
    25. End Function


    An welcher Stelle muss ich wie ändern, dass nicht das 'normale' Adressbuch zum Einsatz kommt, sondern das öffentliche, namens "Firmenkontakte"?

    Vielen Dank für Eure Hilfe! :)

    Gruß aus Bremen,
    Hanseat
    Danke für Deine schnelle Antwort, Henry! :thumbup:

    Im Prinzip ganz einfach - aber ich bekomme leider immer noch die Fehlermeldung :
    "System.Runtime.InteropServices.COMException: "Der versuchte Vorgang konnte nicht ausgeführt werden. Ein Objekt wurde nicht gefunden."

    Könnte das am Namen des übergeordneten Folders liegen? Wie heißt das Ding? Im Outlook liegt mein Adressbuch "Firmenkontakte" unter "Meine Kontakte". Wenn ich das eingebe, kommt der o.g. Fehler. Gleiches gilt, wenn ich als Parent eingebe: "Kontakte", "My Contacts", "Contacts", etc. Immer der gleiche Fehler ;(

    Wie kann ich den richtigen Pfad herausfinden???

    Danke schööööön!
    Ich hatte da mal etwas gebastelt um die Pfade der Outlook-Ordner anzuzeigen.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports Outlook = Microsoft.Office.Interop.Outlook
    2. Imports System.Runtime.InteropServices
    3. Public Class Form1
    4. Dim WithEvents OutlookApp As Outlook.Application = CType(Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")), Outlook.Application)
    5. Dim allFolders As New List(Of String)()
    6. Private Sub bt_Suche_Click(sender As System.Object, e As System.EventArgs) Handles bt_Suche.Click
    7. allFolders.Clear()
    8. ListBox1.Items.Clear()
    9. 'getListOfStores()
    10. 'GetListOfFolders()
    11. SearchFolders(tb_Suche.Text, False)
    12. ListBox1.Items.AddRange(allFolders.ToArray)
    13. End Sub
    14. Sub getListOfStores()
    15. Dim ns As Outlook.NameSpace = Nothing
    16. Dim stores As Outlook.Stores = Nothing
    17. Dim store As Outlook.Store = Nothing
    18. Dim storeList As New List(Of String)
    19. Try
    20. ns = OutlookApp.Session
    21. stores = ns.Stores
    22. Dim i As Integer = 1
    23. While i <= stores.Count
    24. store = stores(i)
    25. storeList.Add(String.Format("{0} - {1}", store.DisplayName, (If(store.IsDataFileStore, ".pst", ".ost"))))
    26. If store IsNot Nothing Then
    27. Marshal.ReleaseComObject(store)
    28. End If
    29. i += 1
    30. End While
    31. 'MessageBox.Show(storeList)
    32. ListBox1.Items.AddRange(storeList.ToArray)
    33. Finally
    34. If stores IsNot Nothing Then
    35. Marshal.ReleaseComObject(stores)
    36. End If
    37. If ns IsNot Nothing Then
    38. Marshal.ReleaseComObject(ns)
    39. End If
    40. End Try
    41. End Sub
    42. Sub GetListOfFolders()
    43. Dim ns As Outlook.NameSpace = Nothing
    44. Dim stores As Outlook.Stores = Nothing
    45. Dim store As Outlook.Store = Nothing
    46. Dim rootFolder As Outlook.MAPIFolder = Nothing
    47. Try
    48. ns = OutlookApp.Session
    49. stores = ns.Stores
    50. For Each store In stores
    51. rootFolder = store.GetRootFolder()
    52. EnumerateFolders(CType(rootFolder, Outlook.Folder))
    53. Next
    54. Finally
    55. If rootFolder IsNot Nothing Then
    56. Marshal.ReleaseComObject(rootFolder)
    57. End If
    58. If store IsNot Nothing Then
    59. Marshal.ReleaseComObject(store)
    60. End If
    61. If stores IsNot Nothing Then
    62. Marshal.ReleaseComObject(stores)
    63. End If
    64. If ns IsNot Nothing Then
    65. Marshal.ReleaseComObject(ns)
    66. End If
    67. End Try
    68. End Sub
    69. Sub SearchFolders(ByVal FolderName As String, Optional OnlyFirst As Boolean = False)
    70. Dim ns As Outlook.NameSpace = Nothing
    71. Dim stores As Outlook.Stores = Nothing
    72. Dim store As Outlook.Store = Nothing
    73. Dim rootFolder As Outlook.MAPIFolder = Nothing
    74. Dim folders As Outlook.Folders = Nothing
    75. Dim Found As Boolean = False
    76. Try
    77. ns = OutlookApp.Session
    78. stores = ns.Stores
    79. For Each store In stores
    80. rootFolder = store.GetRootFolder()
    81. If Not Found Then
    82. EnumerateFolders(CType(rootFolder, Outlook.Folder), FolderName, Found, OnlyFirst)
    83. Else
    84. Exit For
    85. End If
    86. Next
    87. Finally
    88. If folders IsNot Nothing Then
    89. Marshal.ReleaseComObject(folders)
    90. End If
    91. If rootFolder IsNot Nothing Then
    92. Marshal.ReleaseComObject(rootFolder)
    93. End If
    94. If store IsNot Nothing Then
    95. Marshal.ReleaseComObject(store)
    96. End If
    97. If stores IsNot Nothing Then
    98. Marshal.ReleaseComObject(stores)
    99. End If
    100. If ns IsNot Nothing Then
    101. Marshal.ReleaseComObject(ns)
    102. End If
    103. End Try
    104. End Sub
    105. ' Uses recursion to enumerate Outlook subfolders.
    106. Private Sub EnumerateFolders(folder As Outlook.Folder)
    107. Dim childFolders As Outlook.Folders = folder.Folders
    108. If childFolders.Count > 0 Then
    109. For Each childFolder As Outlook.Folder In childFolders
    110. ' Write the folder path.
    111. 'Debug.WriteLine(childFolder.FolderPath)
    112. allFolders.Add(childFolder.FolderPath)
    113. ' Call EnumerateFolders using childFolder.
    114. EnumerateFolders(childFolder)
    115. If childFolder IsNot Nothing Then
    116. Marshal.ReleaseComObject(childFolder)
    117. End If
    118. Next
    119. End If
    120. End Sub
    121. ' Uses recursion to enumerate Outlook subfolders.
    122. Private Sub EnumerateFolders(ByVal folder As Outlook.Folder, ByVal FolderName As String, ByRef Found As Boolean, Optional OnlyFirst As Boolean = False)
    123. Dim childFolders As Outlook.Folders = folder.Folders
    124. If childFolders.Count > 0 Then
    125. For Each childFolder As Outlook.Folder In childFolders
    126. If Found And OnlyFirst Then Exit Sub
    127. ' Write the folder path.
    128. 'Debug.WriteLine(childFolder.FolderPath)
    129. Found = childFolder.Name.ToLower Like FolderName.ToLower
    130. If Found Then
    131. 'MessageBox.Show(childFolder.FolderPath)
    132. allFolders.Add(childFolder.FolderPath)
    133. If OnlyFirst Then Exit Sub
    134. ElseIf Not Found Then
    135. ' Call EnumerateFolders using childFolder.
    136. EnumerateFolders(childFolder, FolderName, Found, OnlyFirst)
    137. If childFolder IsNot Nothing Then
    138. Marshal.ReleaseComObject(childFolder)
    139. End If
    140. End If
    141. Next
    142. End If
    143. End Sub
    144. End Class
    Vielen Dank, Henry,

    ich habe das installiert, meine Pfade ausgelesen und in allen(!) Variationsmöglichkeiten getestet. Ich habe Office repariert, Profile gepflegt, 1000* das System hoch und runter gefahren.
    Das Ergebnis: nix. nada. null :(

    Jetzt weiß ich echt nicht mehr weiter. Vielleicht muss ich das ganze Projekt komplett umstellen, weg von Outlookkontakten hin zu einer separaten Datenbank in MySQL, oder so. Es ist echt ärgerlich...

    Aber Dir vielen Dank für Deine Hilfe und Mühe!

    Einen schönen Mai und viele Grüße,
    Hanseat