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
|
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop
Public Class Contacts
' Déclaration des objets Outlook à l'aide de l'interface outlook.application
Dim ObjOutlook As New Outlook.Application
' le namespace d'Outlook sera la session en cours
Dim ObjNS As Outlook.NameSpace
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
' Initialisation des objets déclarées dans form load
ObjOutlook = New Outlook.Application
ObjNS = ObjOutlook.Session
'Avoir le dossier des contacts
Dim ObjAdressList As Outlook.MAPIFolder
ObjAdressList = _
ObjNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
'Avoir tous les contacts
Dim ObjItems As Outlook.Items = ObjAdressList.Items
Dim ObjContact As Outlook.ContactItem
'Boucle pour parcouris tous les contacts et les ajoutés dans la liste déroulante
For j = 1 To ObjItems.Count
ObjContact = ObjItems(j)
ComboBox1.Items.Add(ObjContact.FullName)
Next
'Vider la mèmoire
ObjContact = Nothing
ObjAdressList = Nothing
ObjItems = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Afficher le premier contact au démarrage
ComboBox1.SelectedIndex = 0
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If ComboBox1.SelectedValue ListBox1.Items.ToString Then
inserer_DB.Enabled = True
Else
inserer_DB.Enabled = False
End If
' Avoir le dossier des contacts
Dim ObjAdressList As Outlook.MAPIFolder
ObjAdressList = ObjNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
'Trouver le contact par son nom
Dim ObjItems As Outlook.Items = ObjAdressList.Items
Dim ObjetContact As Outlook.ContactItem = ObjItems.Find("[FullName] = '" & _
ComboBox1.SelectedItem.ToString() & "'")
'Afficher les détaills dans les labels
Agence.Text = ObjetContact.CompanyName
Car.Text = ObjetContact.JobTitle
Email1.Text = ObjetContact.Email1Address
Email2.Text = ObjetContact.Email2Address
Email3.Text = ObjetContact.Email3Address
Fax.Text = ObjetContact.BusinessFaxNumber
AdrT.Text = ObjetContact.BusinessAddress
AdrD.Text = ObjetContact.HomeAddress
TelC.Text = ObjetContact.BusinessTelephoneNumber
TelP.Text = ObjetContact.MobileTelephoneNumber
'Vider la mèmoire
ObjAdressList = Nothing
ObjetContact = Nothing
ObjItems = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Shared _chaine As String = "Data Source=SE-54E5F33BF2E1\SQLEXPRESS;Initial Catalog=MJtest1;Integrated Security=SSPI;"
Private Sub inserer_DB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inserer_DB.Click
Try
Dim con As New SqlConnection(_chaine)
con.Open()
Dim cmd As New SqlCommand("insert into Contact_Outlook values('" & ComboBox1.Text & "','" & Agence.Text & "','" & Car.Text & "','" & Email1.Text & "','" & Email2.Text & "','" & Email3.Text & "','" & TelC.Text & "','" & TelP.Text & "','" & Fax.Text & "','" & AdrT.Text & "','" & AdrD.Text & "')", con)
If cmd.ExecuteNonQuery() Then
MsgBox("Insertion réussie")
Else
MsgBox("Insertion interrompue")
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class |
Partager