More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 18-Dec-2018 14:17

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Contacts as XML
Description: VBA Script that generates a simple XML document in the same format as an Access XML export.
Date: 03-Mar-2003  09:02
Code level: intermediate
Code area: Basic Outlook
Posted by: Randy Haben
This message is displayed as VB.NET
 
Sub ContactsToXML()
  Dim xmlHeader  As String
  Dim olApp As Outlook.Application
  Dim objName As NameSpace
  Dim Folder As MAPIFolder
  Dim objContact As Variant
  'Dim objContact As Outlook.ContactItem
  Dim objMailItem As Outlook.MailItem
  Dim objAppointment As Outlook.AppointmentItem
  Dim i As Integer
  
              
  Set olApp = Outlook.Application
  Set objName = olApp.GetNamespace("MAPI")
  Set Folder = objName.GetDefaultFolder(olFolderContacts)
  Set Folder = Folder.Folders.Item(2)
        
      
  Open "C:\Documents and Settings\Anyobdy\My Documents\contacts.xml" For Output As #1

  Print #1, "<?xml version=""1.0""?>"
  Print #1, "<?xml-stylesheet type=""text/xsl"" href=""contacts.xsl""?>"
  Print #1, "<dataroot xmlns:od=""urn:schemas-microsoft-com:officedata"">"
  
  On Error Resume Next
  
  For Each Item In Folder.Items
  
  Set objAppointment = Item
    Print #1, "  <Contacts>"
    
    Call PrintXMLElement("FullName", Item.FullName)
    Call PrintXMLElement("FirstName", Item.FirstName)
    Call PrintXMLElement("LastName", Item.LastName)
    Call PrintXMLElement("BusinessPhone", Item.BusinessTelephoneNumber)
    Call PrintXMLElement("Department", Item.Department)
    Call PrintXMLElement("Categories", Item.Categories)
    Call PrintXMLElement("EmailAddress", Item.Email1Address)
    
    If Len(Item.Body) > 0 Then
      Print #1, "    <Notes><![CDATA[" & Item.Body & "]]></Notes>"
    End If
        
    Print #1, "  </Contacts>"
  Next Item
      
  Print #1, "</dataroot>"
  
  Close #1

End Sub

Sub PrintXMLElement(Name, Value)
  If Len(Value) > 0 Then
    Print #1, "    <" & Name & ">" & Value & "</" & Name & ">"
  End If
End Sub
All 17comments
Page [ 1 2 Next >>  
  10-Mar-2003  16:21   
I have tried the code above but it errors out on the line "Set Folder = Folder.Folders.Item(2)". The error I receive is "Array Index out of bounds." What would be causing thsi?

 
  11-Mar-2003  19:41   
In the context of the above code, Folder.Folders.Item(2) would refer to the 2nd subfolder under the default Contacts folder. If you want to work with contacts in your Contacts folder, you don't need that statement.
  11-Jun-2003  10:36   
I apologize. I didn't sanitize the code veru well before posting it. There is another extraneous line. You can delete the assignment to objAppointment -- it was experimental. Also, it's good practice (though not strictly necessary) to Set all object variables to Nothing when you're finished with them. This explicitly releases the associated object interfaces.
  29-Aug-2003  04:28   
Once you've exported as XML - how do you get it back into Outlook?
  08-Sep-2003  21:38   
You'd need to write a reverse routine to map XML elements to Outlook fields. Or see if Access or Excel can handle the XML file, then import from there.
  11-Aug-2004  16:49   
For another example of exporting contacts to XML, see http://www.topxml.com/snippetcentral/main.asp?view=viewsnippet&lang=xml&id=v20020414223234 (Thanks to Outlook MVP Jessie Louise McClennan for the link)
  17-Mar-2005  04:40   
can anyone help me in importing custom fields in contacts usign xml
  17-Mar-2005  08:27   
Sumit, instead of Item.propname for a built-in field, you'd use Item.UserProperties("propname") for a custom field. See http://www.outlookcode.com/d/propsyntax.htm
  05-Aug-2005  14:02   
If you wanted to do the same thing with Tasks, how would you change the above code?
  08-Aug-2005  08:56   
YOu would get the Tasks folder instead of the Contacts folder in your GetDefaultFolder expression and set the arguments for the PrintXMLElement statements to use the TaskItem properties that you want to export.
Page [ 1 2 Next >>