Importer contacts outlook vers excel

Regueiro

XLDnaute Impliqué
Bonsoir Le Forum
J'ai trouver ce code pour importer mes Contacts Outlook dans ma BDD Adresse.
Le code à priori marche et les contacts sont transférés dans ma BDD.

Code:
Sub LectureContacts()
    Set olApp = CreateObject("Outlook.Application")
    Set olns = olApp.GetNamespace("MAPI")
    Set olfFolder = olns.GetDefaultFolder(10)
    ligne = 3
    On Error Resume Next               ' contacts incomplets
    For Each i In olfFolder.Items
       Cells(ligne, 1) = i.CompanyName                    '=Société
       Cells(ligne, 2) = i.LastName
       Cells(ligne, 3) = i.Email1Address
       Cells(ligne, 4) = i.Categories
       ligne = ligne + 1
    Next i
   On Error GoTo 0
   [A2].Sort Key1:=[A2], Header:=xlYes
End Sub

Par contre j'ai un bug avec le tri des données.
Pourriez-vous me dire ou est le problème.
En fait j'aimerais les données soit triées automatiquement depuis la Cellule A3 sans les titres
Et ensuite par la Cellule B3 et C3
Capture.jpg
Merci de votre aide
 

Pièces jointes

  • Capture.jpg
    Capture.jpg
    23.5 KB · Affichages: 134
  • Capture.jpg
    Capture.jpg
    23.5 KB · Affichages: 128

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonsoir le Forum
Le problème de tri est résolu je ne sais pas comment ?
Mais j'aimerais également que le trie se fasse es colonnes suivantes.
C'est-à-dire : Trie colonne 1 + 4 + 5


Code:
Sub LectureContacts()
    Set olApp = CreateObject("Outlook.Application")
    Set olns = olApp.GetNamespace("MAPI")
    Set olfFolder = olns.GetDefaultFolder(10)
    ligne = 3
    On Error Resume Next               ' contacts incomplets
    For Each i In olfFolder.Items
       Cells(ligne, 1) = i.CompanyName         '=Société
       Cells(ligne, 2) = i.JobTitle             '=Fonction
       Cells(ligne, 3) = i.Title                '=Titre
       Cells(ligne, 4) = i.LastName             '=Nom
       Cells(ligne, 5) = i.FirstName                   '=Prénom
       Cells(ligne, 6) = i.BusinessAddressStreet       '=Adresse
       Cells(ligne, 8) = i.BusinessAddressCity         '=Ville
       Cells(ligne, 7) = i.BusinessAddressPostalCode   '=Code Postal
       Cells(ligne, 9) = i.BusinessAddressState         '=Département
       Cells(ligne, 10) = i.BusinessAddressCountry      '=Pays
       Cells(ligne, 11) = i.BusinessTelephoneNumber    '=n° Tel
       Cells(ligne, 12) = i.BusinessFaxNumber          '=n° Fax
       Cells(ligne, 13) = i.MobileTelephoneNumber      '=n° GSM
       Cells(ligne, 14) = i.Email1Address               '=EMail
       Cells(ligne, 15) = i.Email1Address               '=Site WEB
       'Cells(ligne, 15) = i.Categories
       ligne = ligne + 1
    Next i
   On Error GoTo 0
   [A2].Sort Key1:=[A2], Header:=xlYes
End Sub
Merci de votre aide.
Pour ce code.

Entretemps j'ai découvert le code qu'il me fallait pour mon application.
Comme d'habitude sur le Site de boisgontier.

Code:
Dim Tbl()
Private Sub UserForm_Initialize()
    Dim Temp()
    Set olApp = CreateObject("Outlook.Application")
    Set olns = olApp.GetNamespace("MAPI")
    Set olfFolder = olns.GetDefaultFolder(10)
    n = 0
    On Error Resume Next          '  Contacts incomplets
    For Each i In olfFolder.Items
      ReDim Preserve Tbl(0 To 6, 0 To n)    'modif 6 avant 3
      Tbl(0, n) = i.CompanyName
      Tbl(1, n) = i.LastName
      Tbl(2, n) = i.FirstName
      Tbl(4, n) = i.BusinessAddressStreet
      Tbl(5, n) = i.BusinessAddressPostalCode
      Tbl(6, n) = i.BusinessAddressCity
      Tbl(3, n) = i.Categories
      n = n + 1
    Next
    On Error GoTo 0
    Call triQ(Tbl, 0, n - 1)
    Me.ListBox1.List = Application.Transpose(Tbl)
    Set Mondico = CreateObject("Scripting.Dictionary")
    Mondico.Add "(tous)", "(tous)"
    For i = 0 To UBound(Tbl, 2)
      Tmp = Split(Tbl(3, i), ";")
      For k = LBound(Tmp) To UBound(Tmp)
        If Not Mondico.Exists(Trim(Tmp(k))) Then Mondico.Add Trim(Tmp(k)), Trim(Tmp(k))
      Next k
    Next i
    Me.ChoixCatégorie.List = Mondico.Items
    Me.ChoixCatégorie = "(tous)"
End Sub
Sub triQ(a(), gauc, droi)
' Quick sort
 ref = a(0, (gauc + droi) \ 2)
 g = gauc: d = droi
 Do
     Do While a(0, g) < ref: g = g + 1: Loop
     Do While ref < a(0, d): d = d - 1: Loop
     If g <= d Then
       Temp = a(0, g): a(0, g) = a(0, d): a(0, d) = Temp
       Temp = a(1, g): a(1, g) = a(1, d): a(1, d) = Temp
       Temp = a(2, g): a(2, g) = a(2, d): a(2, d) = Temp
       Temp = a(3, g): a(3, g) = a(3, d): a(3, d) = Temp
       g = g + 1: d = d - 1
     End If
 Loop While g <= d
 If g < droi Then Call triQ(a, g, droi)
 If gauc < d Then Call triQ(a, gauc, d)
End Sub
Private Sub ListBox1_Click()
  On Error Resume Next
  [L17] = ListBox1              'JRE 11.03.2012
  [L18] = ListBox1.Column(1)
  [L19] = ListBox1.Column(2)
  [L20] = ListBox1.Column(3)
End Sub
Private Sub ChoixCatégorie_Change()
  Dim Temp()
  If Me.ChoixCatégorie = "(tous)" Then
    Me.ListBox1.List = Application.Transpose(Tbl)
  Else
    j = 0
    For i = 0 To UBound(Tbl, 2)
      If InStr(Tbl(3, i), Me.ChoixCatégorie) > 0 Then
         ReDim Preserve Temp(0 To 6, 0 To j)
         Temp(0, j) = Tbl(0, i): Temp(1, j) = Tbl(1, i)
         Temp(2, j) = Tbl(2, i): Temp(3, j) = Tbl(3, i)
         j = j + 1
      End If
    Next i
    If UBound(Temp, 2) > 0 Then
      Me.ListBox1.List = Application.Transpose(Temp)
    Else
      ReDim Preserve Temp(0 To 6, 0 To j)
      Temp(0, j) = "": Temp(1, j) = "": Temp(2, j) = "": Temp(3, j) = ""
      Me.ListBox1.List = Application.Transpose(Temp)
    End If
  End If
End Sub

En fait j'aimerais rajouter 6 colonnes sur la listbox au lieu des 3 prévus dans le code de boisgontier.
Comment dois-je faire.
Merci de votra Aide.
A+
 

francedemo

XLDnaute Occasionnel
Re : Importer contacts outlook vers excel

bonjour,
j'utilise aussi un fichier issu de contacts d'outlook
pour le tri:
Code:
Sub TriContacts()

Dim DerLigne As Long
Dim DerColonne As Long
Dim LigneTitres As Range
Dim ZoneTri As Range

'===Dévalider la mise à jour des affichages
Application.ScreenUpdating = False
Application.DisplayAlerts = False
 
'===Valider les variables
With Sheets("Contacts")
    .Activate
    DerLigne = .[A65536].End(xlUp).Row
    DerColonne = .[A1].End(xlToRight).Column
    Set LigneTitres = .[Ligne_Titres]
    Set ZoneTri = .Range(Cells(1, 1), Cells(DerLigne, DerColonne))
    Selection.AutoFilter
    '===Trier la zone de contacts par "Société" / "Fonction" / "Nom"
    ZoneTri.Sort _
        Key1:=[A2], _
        Order1:=xlAscending, _
        Key2:=[B2], _
        Order2:=xlAscending, _
        Key3:=[D2], _
        Order3:=xlAscending, _
        Header:=xlGuess, _
        OrderCustom:=1, _
        MatchCase:=False, _
        Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, _
        DataOption2:=xlSortNormal
    '===Supprimer les lignes vides en fin de fichier
    '.Range(Rows(DerLigne + 1), Rows(DerLigne + 5)).Delete
    .Range("A" & DerLigne + 1 & ":A" & DerLigne + 5).Delete
    LigneTitres.Select
    Selection.AutoFilter
    .EnableAutoFilter = True
    '===Mettre en forme le tableau + Code postaux + Calcul Nb caratères
    .[A2].AutoFormat _
        Format:=xlRangeAutoFormatList1, _
        Number:=True, _
        Font:=True, _
        Alignment:=True, _
        Border:=True, _
        Pattern:=True, _
        Width:=True
    .Range("H2:H" & DerLigne).NumberFormat = "00000"
    .Range("Q1:Q" & DerLigne).FillDown
End With
'===Valider la mise à jour des affichages
Application.ScreenUpdating = True
Application.DisplayAlerts = True
 
End Sub

à adapter avec ton nom de feuille et tes zone de tri

à+
 

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonsoir Le Forum
Salut Francedemo
Ton code marche parfaitement je te remercie.
Toujours concernant la 2ème partie de mon PostN° 3 ?
Quelqu'un aurait-il la solution ou bien m'indiquer dans quelle voie je dois aller.
Merci
A+
 

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonsoir Le Forum
Voilà après plusieurs heures de recherche et tatonnement
J'ai réussie ( c'est un exploit pour moi ) à adapter le code Boisgontier
pour me permettre d'avoir 12 colonnes dans ma listbox
Je vous mais le code ci-dessous.
Mais j'ai encore une requête.
J'essaye de mettre une nouvelle combobox

Private Sub ChoixCatégorie_Change()
pour faire un tri sur la 1ère colonne mais je ne trouve pas.

Code:
Dim Tbl()

Private Sub UserForm_Initialize()
    Dim Temp()
    Set olApp = CreateObject("Outlook.Application")
    Set olns = olApp.GetNamespace("MAPI")
    Set olfFolder = olns.GetDefaultFolder(10)
    n = 0
    On Error Resume Next          '  Contacts incomplets
    For Each i In olfFolder.Items
      ReDim Preserve Tbl(0 To 12, 0 To n)
      'modif 0 To 12(Avant 0 to 3)et propriétés ListBox ColumnCount 10 et les largeurs des colonnes dans
      'ColumnWidths
      Tbl(0, n) = i.CompanyName
      Tbl(1, n) = i.LastName & " " & i.FirstName
      Tbl(2, n) = i.Title                           'Titre M. - MM, etc
      Tbl(3, n) = i.Categories
      Tbl(4, n) = i.BusinessAddressStreet
      Tbl(5, n) = i.BusinessAddressPostalCode ';i.BusinessAdressCity
      Tbl(6, n) = i.BusinessAddressCity
      Tbl(7, n) = i.BusinessAddressState         '=Département
      Tbl(8, n) = i.BusinessAddressCountry      '=Pays
      Tbl(9, n) = i.BusinessFaxNumber
      Tbl(10, n) = i.Email1Address
      Tbl(11, n) = i.MobileTelephoneNumber
      'Tbl(7, n) = i.BusinessTelephoneNumber
      'Tbl(8, n) = i.BusinessFaxNumber
      'Tbl(9, n) = i.MobileTelephoneNumber
      'Tbl(10, n) = i.Email1Address
      'Tbl(11, n) = i.WebPage
      n = n + 1
    Next
    On Error GoTo 0
    Call triQ(Tbl, 0, n - 1)
    Me.ListBox1.List = Application.Transpose(Tbl)
    Set Mondico = CreateObject("Scripting.Dictionary")
    Mondico.Add "(tous)", "(tous)"
    For i = 0 To UBound(Tbl, 2)
      Tmp = Split(Tbl(3, i), ";")
      For k = LBound(Tmp) To UBound(Tmp)
        If Not Mondico.Exists(Trim(Tmp(k))) Then Mondico.Add Trim(Tmp(k)), Trim(Tmp(k))
      Next k
    Next i
    Me.ChoixCatégorie.List = Mondico.Items
    Me.ChoixCatégorie = "(tous)"
End Sub
Sub triQ(a(), gauc, droi)
' Quick sort
 ref = a(0, (gauc + droi) \ 2)
 g = gauc: d = droi
 Do
     Do While a(0, g) < ref: g = g + 1: Loop
     Do While ref < a(0, d): d = d - 1: Loop
     If g <= d Then
       Temp = a(0, g): a(0, g) = a(0, d): a(0, d) = Temp
       Temp = a(1, g): a(1, g) = a(1, d): a(1, d) = Temp
       Temp = a(2, g): a(2, g) = a(2, d): a(2, d) = Temp
       Temp = a(3, g): a(3, g) = a(3, d): a(3, d) = Temp
       Temp = a(4, g): a(4, g) = a(4, d): a(4, d) = Temp
       Temp = a(5, g): a(5, g) = a(5, d): a(5, d) = Temp
       Temp = a(6, g): a(6, g) = a(6, d): a(6, d) = Temp
       Temp = a(7, g): a(7, g) = a(7, d): a(7, d) = Temp
       Temp = a(8, g): a(8, g) = a(8, d): a(8, d) = Temp
       Temp = a(9, g): a(9, g) = a(9, d): a(9, d) = Temp
       Temp = a(10, g): a(10, g) = a(10, d): a(10, d) = Temp
       Temp = a(11, g): a(11, g) = a(11, d): a(11, d) = Temp
       g = g + 1: d = d - 1
     End If
 Loop While g <= d
 If g < droi Then Call triQ(a, g, droi)
 If gauc < d Then Call triQ(a, gauc, d)
End Sub
Private Sub ListBox1_Click()
  On Error Resume Next
  [L17] = ListBox1.Column(0)                                           'ENTREPRISE
  [L18] = ListBox1.Column(2) & " " & ListBox1.Column(1)                'TITRE NOM ET PRENOM
  [L19] = ListBox1.Column(4)                                            'ADRESSE
  [L20] = ListBox1.Column(5) & " " & ListBox1.Column(6)                 'CP VILLE
  [L21] = ListBox1.Column(7) & " - " & ListBox1.Column(8)               'CANTON - PAYS
  [P18] = " F: " & ListBox1.Column(9)                                'FAX
  [P19] = " E: " & ListBox1.Column(10)                             'EMAIL
  [P20] = " N: " & ListBox1.Column(11)                             'NATEL
End Sub
Private Sub ChoixCatégorie_Change()
  Dim Temp()
  If Me.ChoixCatégorie = "(tous)" Then
    Me.ListBox1.List = Application.Transpose(Tbl)
  Else
    j = 0
    For i = 0 To UBound(Tbl, 2)
      If InStr(Tbl(3, i), Me.ChoixCatégorie) > 0 Then
         ReDim Preserve Temp(0 To 12, 0 To j)
         Temp(0, j) = Tbl(0, i): Temp(1, j) = Tbl(1, i)
         Temp(2, j) = Tbl(2, i): Temp(3, j) = Tbl(3, i)
         Temp(4, j) = Tbl(4, i): Temp(5, j) = Tbl(5, i)
         Temp(6, j) = Tbl(6, i): Temp(7, j) = Tbl(7, i)
         Temp(8, j) = Tbl(8, i): Temp(9, j) = Tbl(9, i)
         Temp(10, j) = Tbl(10, i): Temp(11, j) = Tbl(11, i)
         j = j + 1
      End If
    Next i
    If UBound(Temp, 2) > 0 Then
      Me.ListBox1.List = Application.Transpose(Temp)
    Else
      ReDim Preserve Temp(0 To 12, 0 To j)
      Temp(0, j) = "": Temp(1, j) = "": Temp(2, j) = "": Temp(3, j) = "": Temp(4, j) = ""
      Temp(5, j) = "": Temp(6, j) = "": Temp(7, j) = "": Temp(8, j) = "": Temp(9, j) = ""
      Temp(10, j) = "": Temp(11, j) = ""
      Me.ListBox1.List = Application.Transpose(Temp)
    End If
  End If
End Sub
Private Sub ChoixEntreprise_Change()
  Dim Temp()
  If Me.ChoixEntreprise = "(tous)" Then
    Me.ListBox1.List = Application.Transpose(Tbl)
  Else
    j = 0
    For i = 0 To UBound(Tbl, 2)                         'changer 2 par 0
      If InStr(Tbl(1, i), Me.ChoixEntreprise) > 0 Then       'changer 3 par 0
         ReDim Preserve Temp(0 To 12, 0 To j)
         Temp(0, j) = Tbl(0, i): Temp(1, j) = Tbl(1, i)
         Temp(2, j) = Tbl(2, i): Temp(3, j) = Tbl(3, i)
         Temp(4, j) = Tbl(4, i): Temp(5, j) = Tbl(5, i)
         Temp(6, j) = Tbl(6, i): Temp(7, j) = Tbl(7, i)
         Temp(8, j) = Tbl(8, i): Temp(9, j) = Tbl(9, i)
         Temp(10, j) = Tbl(10, i): Temp(11, j) = Tbl(11, i)
         j = j + 1
      End If
    Next i
    If UBound(Temp, 1) > 0 Then                             'changer 2 par 0
      Me.ListBox1.List = Application.Transpose(Temp)
    Else
      ReDim Preserve Temp(0 To 12, 0 To j)
      Temp(0, j) = "": Temp(1, j) = "": Temp(2, j) = "": Temp(3, j) = "": Temp(4, j) = ""
      Temp(5, j) = "": Temp(6, j) = "": Temp(7, j) = "": Temp(8, j) = "": Temp(9, j) = ""
      Temp(10, j) = "": Temp(11, j) = ""
      Me.ListBox1.List = Application.Transpose(Temp)
    End If
  End If
End Sub


Et si quelqu'un aurais une idée pour rajouter une combobox ou autre pour faire une recherche
par nom pour simplifier la recherche.

Merci de votre Aide
Bonne soirée
A+
 

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonjour A tous.
Je cherche de l'aide pour mon Post N° 8
Il y aurait-il une personne qui pourrait m'aiguiller ?
Je ne sais pas si M. Boisgontier serait dans les parages ?

Merci
Bonne Appétit
A+
 

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonjour à Tous
Salut Yaloo
Voilà le fichier
Merci A+
 

Pièces jointes

  • FORMESSAI01.xlsm
    29.7 KB · Affichages: 105
  • FORMESSAI01.xlsm
    29.7 KB · Affichages: 110
  • FORMESSAI01.xlsm
    29.7 KB · Affichages: 107

Yaloo

XLDnaute Barbatruc
Re : Importer contacts outlook vers excel

Re,

Avec ça peut être, j'ai rajouté un bout de code en fin dans l'initialisation, un combobox pour le nom et son code.

A voir, chez moi ça fonctionne.

A+
 

Pièces jointes

  • FORMESSAI01.xlsm
    31.1 KB · Affichages: 100
  • FORMESSAI01.xlsm
    31.1 KB · Affichages: 107
  • FORMESSAI01.xlsm
    31.1 KB · Affichages: 98

Regueiro

XLDnaute Impliqué
Re : Importer contacts outlook vers excel

Bonsoir le Forum
Salut Yaloo
Merci beaucoup, j'étais proche du but, mais je ne savais pas qu'il fallait rajouter qqch dans Private Sub UserForm_Initialize()
Code:
    '**************************************************************************************************
     'POUR ALIMENTER LA COMBOBOX - CHOIXNOM
    Set Mondico = CreateObject("Scripting.Dictionary")
    Mondico.Add "(tous)", "(tous)"
    For i = 0 To UBound(Tbl, 2)
      Tmp = Split(Tbl(1, i), ";")                           'CHANGER TABLEAU 1
      For k = LBound(Tmp) To UBound(Tmp)
        If Not Mondico.Exists(Trim(Tmp(k))) Then Mondico.Add Trim(Tmp(k)), Trim(Tmp(k))
      Next k
    Next i
    Me.ChoixNom.List = Mondico.Items
    Me.ChoixNom = "(tous)"
    '*********************************************************************************************
     'POUR ALIMENTER LA 3EME COMBOBOX - CHOIXENTREPRISE
    Set Mondico = CreateObject("Scripting.Dictionary")
    Mondico.Add "(tous)", "(tous)"
    For i = 0 To UBound(Tbl, 2)
      Tmp = Split(Tbl(0, i), ";")                           'CHANGER TABLEAU 0
      For k = LBound(Tmp) To UBound(Tmp)
        If Not Mondico.Exists(Trim(Tmp(k))) Then Mondico.Add Trim(Tmp(k)), Trim(Tmp(k))
      Next k
    Next i
    Me.ChoixEntreprise.List = Mondico.Items
    Me.ChoixEntreprise = "(tous)"

Encore quelques Questions pour finaliser mon programme :
1. Pour Avoir la colonne 1 + 2 + 3 dans l'ordre Alphabétique
2. Si je dois aller chercher un autre Groupe de Contact dans Outlook, par exemple
j'ai le groupe Contacts suggérés dans la même arborescence
3. Comme je dois appliquer mon programme au travail si nous avons des contacts partagés sur
le réseau de type "Contacts Achats"

Je crois que c'est dans ce code là, mais je ne sais pas comment faire
Code:
Private Sub UserForm_Initialize()
    Dim Temp()
    Set olApp = CreateObject("Outlook.Application")
    Set olns = olApp.GetNamespace("MAPI")
    Set olfFolder = olns.GetDefaultFolder(10)
MErci Encore
A+
 

Discussions similaires

Statistiques des forums

Discussions
312 209
Messages
2 086 271
Membres
103 168
dernier inscrit
isidore33