Makale Özeti

Bu makalede Microsoft Outlook’un klasörlerinden “Contacts” daki verilere erişmeyi ve bunları XML’e çevirmeyi göreceğiz. Siz de bu şekilde diğer klasörlere (inbox,outbox,drafts...) ulaşabilirsiniz.

Makale

 

OUTLOOK CONTACTS’I XML’E ÇEVİRELİM

   Bu makalede Microsoft Outlook’un klasörlerinden “Contacts” daki verilere erişmeyi ve bunları XML’e çevirmeyi göreceğiz. Siz de bu şekilde diğer klasörlere (inbox,outbox,drafts...) ulaşabilirsiniz.
   Uygulamalarımızda Outlook nesnelerine ulaşmak için:
1. Project” menüsünden “Add Reference…”
2. COM nesnelerinden “Microsoft Outlook 10.0 Object Library” yi ekleyelim.

 

    Bu kütüphane sayesinde Outlook’un bütün nesnelerine ve özelliklerine erişebiliriz. Aşağıdaki şekilden de Outlook’un nesne yapısının nasıl olduğuna bakalım.

 

    

    Uygulamamızda bu nesnelere erişim sırasını da numaralandırarak belirttim.
Kodlamaya geçmeden oluşturacağımız XML dosyamızın yapısına da bakalım:

   <OutlookContacts>
      <contacts>
          <contact>
               ....
               bilgiler
               ....
          </contact>
      </contacts>
   </OutlookContacts>

     Neyi nasıl yapacağımıza ve yaptığımızda ne olacağına karar verdik, şimdi kodumuzu yazalım:

Imports System.Xml
Imports
System.Text

.........

.........

Sub OutlookTOxml(ByVal XMLFileName As String)

Try

'1

Dim oApplication As New Outlook.Application

'2

Dim oNameSpace As Outlook.NameSpace

'3...

oNameSpace = oApplication.GetNamespace("MAPI")

'çalışacağımız folder oluşturulur

Dim folderContacts As Outlook.MAPIFolder

'kullanabileceğimiz diğer folder lar:

'OlDefaultFolders.olFolderCalendar()

'OlDefaultFolders.olFolderContacts()

'OlDefaultFolders.olFolderDeletedItems(

'OlDefaultFolders.olFolderDrafts()

'OlDefaultFolders.olFolderInbox()

'OlDefaultFolders.olFolderJournal()

'OlDefaultFolders.olFolderNotes()

'OlDefaultFolders.olFolderOutbox()

'OlDefaultFolders.olFolderSentMail()

'OlDefaultFolders.olFolderTasks()

'OlDefaultFolders.olPublicFoldersAllPublicFolders()

'folder lardan Contacts seçilir

folderContacts = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderContacts)

Dim items As Object = folderContacts.Items

Dim citem As Outlook.ContactItem

Dim entry As Outlook.DistListItem

'XML dosyamızı oluşturalım

Dim xmlw As New Xml.XmlTextWriter(XMLFileName & ".xml", Encoding.UTF8)

xmlw.WriteStartElement("OutlookContacts")

xmlw.WriteStartElement("Contacts")

xmlw.WriteEndElement()

xmlw.WriteEndElement()

xmlw.Flush()

xmlw.Close()

Dim xml As New Xml.XmlDocument

xml.Load("OutlookContacts.xml")

'xml file

'<OutlookContacts>

'<contacts>

' <contact>

' ....

' ....

' </contact>

'</contacts>

'</OutlookContacts>

For Each citem In items

'Sub Element -- <Contact>

Dim XContacts As XmlNode = xml.CreateElement("Contact")

'-----------------------------VERİLERİ ELEMENTLERE EKLEME--------------------------

'<Fullname>

If Not citem.FullName Is Nothing Then

Dim FullName As XmlNode = xml.CreateElement("FullName")

Dim txtFullname As XmlNode = xml.CreateTextNode(citem.FullName.ToString)

'Fullname i ekle

XContacts.AppendChild(FullName)

FullName.AppendChild(txtFullname)

End If

'<HomeAddress>

If Not citem.HomeAddress Is Nothing Then

Dim HomeAddress As XmlNode = xml.CreateElement("HomeAddress")

Dim txtHomeAddress As XmlNode = xml.CreateTextNode(citem.HomeAddress.ToString)

'HomeAddress i ekle

XContacts.AppendChild(HomeAddress)

HomeAddress.AppendChild(txtHomeAddress)

End If

'<HomeAddressCity>

If Not citem.HomeAddressCity Is Nothing Then

Dim HomeAddressCity As XmlNode = xml.CreateElement("HomeAddressCity")

Dim txtHomeAddressCity As XmlNode = xml.CreateTextNode(citem.HomeAddressCity.ToString)

'HomeAddressCity yi ekle

XContacts.AppendChild(HomeAddressCity)

HomeAddressCity.AppendChild(txtHomeAddressCity)

End If

'<HomeAddressCountry>

If Not citem.HomeAddressCountry Is Nothing Then

Dim HomeAddressCountry As XmlNode = xml.CreateElement("HomeAddressCountry")

Dim txtHomeAddressCountry As XmlNode = xml.CreateTextNode(citem.HomeAddressCountry.ToString)

'HomeAddressCountry yi ekle

XContacts.AppendChild(HomeAddressCountry)

HomeAddressCountry.AppendChild(txtHomeAddressCountry)

End If

'<HomeAddressPostalCode>

If Not citem.HomeAddressPostalCode Is Nothing Then

Dim HomeAddressPostalCode As XmlNode = xml.CreateElement("HomeAddressPostalCode")

Dim txtHomeAddressPostalCode As XmlNode = xml.CreateTextNode(citem.HomeAddressPostalCode.ToString)

'HomeAddressPostalCode u ekle

XContacts.AppendChild(HomeAddressPostalCode)

HomeAddressPostalCode.AppendChild(txtHomeAddressPostalCode)

End If

'<HomeTelephoneNumber>

If Not citem.HomeTelephoneNumber Is Nothing Then

Dim HomeTelephoneNumber As XmlNode = xml.CreateElement("HomeTelephoneNumber")

Dim txtHomeTelephoneNumber As XmlNode = xml.CreateTextNode(citem.HomeTelephoneNumber.ToString)

'HomeTelephoneNumber ı ekle

XContacts.AppendChild(HomeTelephoneNumber)

HomeTelephoneNumber.AppendChild(txtHomeTelephoneNumber)

End If

If Not citem.MobileTelephoneNumber Is Nothing Then

Dim MobileTelephoneNumber As XmlNode = xml.CreateElement("MobileTelephoneNumber")

Dim txtMobileTelephoneNumber As XmlNode = xml.CreateTextNode(citem.MobileTelephoneNumber.ToString)

'MobileTelephoneNumber ı ekle

XContacts.AppendChild(MobileTelephoneNumber)

MobileTelephoneNumber.AppendChild(txtMobileTelephoneNumber)

End If

'<BusinessTelephoneNumber>

If Not citem.BusinessTelephoneNumber Is Nothing Then

Dim BusinessTelephoneNumber As XmlNode = xml.CreateElement("BusinessTelephoneNumber")

Dim txtBusinessTelephoneNumber As XmlNode = xml.CreateTextNode(citem.BusinessTelephoneNumber.ToString)

'BusinessTelephoneNumber ı ekle

XContacts.AppendChild(BusinessTelephoneNumber)

BusinessTelephoneNumber.AppendChild(txtBusinessTelephoneNumber)

End If

'<Email1Address>

If Not citem.Email1Address Is Nothing Then

Dim Email1Address As XmlNode = xml.CreateElement("Email1Address")

Dim txtEmail1Address As XmlNode = xml.CreateTextNode(citem.Email1Address.ToString)

'Email1Address i ekle

XContacts.AppendChild(Email1Address)

Email1Address.AppendChild(txtEmail1Address)

End If

'<Email2Address>

If Not citem.Email2Address Is Nothing Then

Dim Email2Address As XmlNode = xml.CreateElement("Email2Address")

Dim txtEmail2Address As XmlNode = xml.CreateTextNode(citem.Email2Address.ToString)

'Email2Address i ekle

XContacts.AppendChild(Email2Address)

Email2Address.AppendChild(txtEmail2Address)

End If

'-----

'<Email3Address>

If Not citem.Email3Address Is Nothing Then

Dim Email3Address As XmlNode = xml.CreateElement("Email3Address")

Dim txtEmail3Address As XmlNode = xml.CreateTextNode(citem.Email3Address.ToString)

'Email3Address i ekle

XContacts.AppendChild(Email3Address)

Email3Address.AppendChild(txtEmail3Address)

End If

'<Gender>

If Not citem.Gender.ToString = "olUnspecified" Then

Dim Gender As XmlNode = xml.CreateElement("Gender")

Dim txtGender As XmlNode = xml.CreateTextNode(citem.Gender.ToString.ToString)

'Gender i ekle

XContacts.AppendChild(Gender)

Gender.AppendChild(txtGender)

End If

'<PersonalHomePage>

If Not citem.PersonalHomePage Is Nothing Then

Dim PersonalHomePage As XmlNode = xml.CreateElement("PersonalHomePage")

Dim txtPersonalHomePage As XmlNode = xml.CreateTextNode(citem.PersonalHomePage.ToString)

'PersonalHomePage i ekle

XContacts.AppendChild(PersonalHomePage)

PersonalHomePage.AppendChild(txtPersonalHomePage)

End If

'XML dosyasına aktardağımız veriler :

'FullName()

'HomeAddress()

'HomeAddressCity()

'HomeAddressCountry()

'HomeAddressPostalCode()

'HomeTelephoneNumber()

'MobileTelephoneNumber()

'MailingAddress()

'Email1Address()

'Email2Address()

'Email3Address()

'Gender()

'PersonalHomePage()

'----------------------------------------------------------------------------------------------

'Her <Contact> ı <Contacts> a ekleyelim

Dim targetNode As XmlNode = xml.SelectSingleNode("//node()/node()")

targetNode.AppendChild(XContacts)

Next

'XML dosyamızı kaydedelim

xml.Save("OutlookContacts.xml")

'XML dosyamızı görelim

Dim rdr As New IO.StreamReader("OutlookContacts.xml")

TextBox1.Text = rdr.ReadToEnd()

MessageBox.Show("Contacts'taki bilgiler XML'e aktarıldı.", "İşlem tamam...", MessageBoxButtons.OK, MessageBoxIcon.Information)

Dim ds As New DataSet

ds.ReadXml(XMLFileName & ".xml")

If ds.Tables.Count > 0 Then

dg.DataSource = ds.Tables(1)

End If

Catch ex As Xml.XmlException

MessageBox.Show(ex.Message.ToString, "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End Sub

 

      Ben kişi bilgilerinden belli başlı birkaçını aldım tercih sizin; formun load olayında sub’ımızı kullandığımızda karşımıza Outlook’un uyarı ekranı çıkacaktır.

 

 

Burdan “Allow access for...” işaretleyelim ve “yes”...

 


Bu işlemle erişim iznini de verdik, artık sonuca bakalım:

 

   Arkadaşlar bu benim ilk makalem, bu yüzden anlamadığınız yani anlatamadığım bir yer varsa bekliyorum:
Kodları İndirmek için Tıklayınız
   kenanacar@hotmail.com
   msn: maydo74@hotmail.com

KENAN ACAR, MCP


 

Makalenın Kodları