Bonjour à tous!
J'ai encore un défi coriace à vous soumettre!
Dans Outlook, j'ai des contacts. Les champs traditionnels ne me suffisaient pas, j'ai donc créé des champs personnalisés (custom fields) sur un formulaire personnalisé (FORM REGION).
Depuis mon application WPF (visual studio community, visual basic), je veux récupérer les valeurs des différents champs d'un contact.
Je n'ai aucun problème pour le faire pour tous les champs classiques, car ils sont prévus dans Microsoft.Office.Interop.Outlook .
Exemple:
Par contre, quand je veux récupérer les valeurs de mes "custom fields" (qui ne sont pas prévus dans l'interop), je tape "oCt." et mes custom fields n'apparaissent pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 Dim oCt As Outlook.ContactItem = CType(oItem, Outlook.ContactItem) VbNom = oCt.LastName VbPrenom = oCt.FirstName VbEmail = oCt.Email1Address
Je suis allé en mode debug grenouiller dans l'oCt, je ne trouve rien.
J'ai essayé d'ajouter Microsoft.VBE.interop.forms, ça me dit que c'est inutile (déjà contenu dans interop.outlook).
J'ai essayé aussi:, ça ne marche pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part oCt.UserProperties.Find("TypeDeContrat").Value
Quelqu’un aurait une idée?
Merci d'avance !!!
Voici le code complet (simplifié)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 Imports System.Runtime.InteropServices Imports Outlook = Microsoft.Office.Interop.Outlook 'Imports Forms = Microsoft.Vbe.Interop.Forms 'visual studio me dit que c'est inutile Class MainWindow Public VbNom As String 'mes variables dans visual studio que je vais remplir avec les valeurs du contact Public VbPrenom As String Public VbEmail As String Public VbTphMobile As String Public VbTypeDeContrat As String Private Sub button_Click_1(sender As Object, e As RoutedEventArgs) Handles button.Click 'Menu de tout ce qui se passe lorsqu'on clique sur le bouton AutomateOutlook() ' Clean up the unmanaged Outlook COM resources by forcing a garbage ' collection as soon as the calling function is off the stack (at ' which point these objects are no longer rooted). GC.Collect() GC.WaitForPendingFinalizers() ' GC needs to be called twice in order to get the Finalizers called - ' the first time in, it simply makes a list of what is to be finalized, ' the second time in, it actually is finalizing. Only then will the ' object do its automatic ReleaseComObject. GC.Collect() GC.WaitForPendingFinalizers() End Sub Sub AutomateOutlook() Dim missing As Object = Type.Missing Dim oOutlook As Outlook.Application = Nothing Dim oNS As Outlook.NameSpace = Nothing Dim oCtFolder As Outlook.MAPIFolder = Nothing Dim oCts As Outlook.Items = Nothing Dim oMail As Outlook.MailItem = Nothing Try ' Démarre Outlook et se log avec mon profil puis crée une application Outlook. oOutlook = New Outlook.Application() ' Prend le namespace oNS = oOutlook.GetNamespace("MAPI") oNS.Logon("Outlook", missing, False, True) 'Passe en revue tous les contacts. oCtFolder = oNS.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderContacts) oCts = oCtFolder.Items Dim ContactTrouve As Boolean ContactTrouve = False For i As Integer = oCts.Count To 1 Step -1 Dim oItem As Object = oCts(i) 'si le contact choisi est bien un contact et non une liste de distribution If (TypeOf oItem Is Outlook.ContactItem) Then 'conversion correcte par CType d'un contact en objet Dim oCt As Outlook.ContactItem = CType(oItem, Outlook.ContactItem) 'Si le nom et le prénom du contact outlook correspondent à ce qui est saisi dans les cases du formulaire If oCt.LastName = NomVoulu.Text And oCt.FirstName = PrenomVoulu.Text And oCt.LastName <> "" Then ContactTrouve = True 'Récupération des données du contact vers des variables locales. VbNom = oCt.LastName VbPrenom = oCt.FirstName VbEmail = oCt.Email1Address VbTphMobile = oCt.MobileTelephoneNumber 'ICI CA NE MARCHE PAS: impossible de trouver comment accéder à mon champ "TypeDeContrat" VbTypeDeContrat = oCt.UserProperties.Find("TypeDeContrat").Value.ToString End If ' Release the COM object of the Outlook item. Marshal.FinalReleaseComObject(oItem) oItem = Nothing Next If ContactTrouve = False Then MsgBox("Aucun contact trouvé avec cet orthographe précis. Pouvez-vous vérifier sil vous plaît?") End If oNS.Logoff() oOutlook.Quit() Catch ex As Exception MsgBox("AutomateOutlook throws the error: {0}") 'J'ai aussi une erreur ici pour afficher le message d'erreur, tant que j'y suis... 'MsgBox("AutomateOutlook throws the error: {0}", CType(ex.Message, MsgBoxStyle)) Finally ' Manually clean up the explicit unmanaged Outlook COM resources by ' calling Marshal.FinalReleaseComObject on all accessor objects. If Not oMail Is Nothing Then Marshal.FinalReleaseComObject(oMail) oMail = Nothing End If If Not oCts Is Nothing Then Marshal.FinalReleaseComObject(oCts) oCts = Nothing End If If Not oCtFolder Is Nothing Then Marshal.FinalReleaseComObject(oCtFolder) oCtFolder = Nothing End If If Not oNS Is Nothing Then Marshal.FinalReleaseComObject(oNS) oNS = Nothing End If If Not oOutlook Is Nothing Then Marshal.FinalReleaseComObject(oOutlook) oOutlook = Nothing End If End Try End Sub End Class
Partager