none
Exporting Custom Fields from Outlook Contacts Using VB Script RRS feed

  • Question

  • I have code that successfully exports outlook contact information to a csv file but I cannot see how I can export custom fields.

    Any suggestions?

    Here is my code:

    Sub SaveContactsinFile()
    
    On Error GoTo ErrorHandling
    
    Dim MyCount As Long
    MyCount = 1
    Dim MyType As String
    
        Dim objOutlook As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objFolder As Outlook.MAPIFolder
        Dim olItems As Outlook.Items
        Dim olContact As Outlook.ContactItem
        
       Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
       Dim strFile As String
       Dim sName As String
      
            Set objNS = Application.GetNamespace("MAPI")
    
    ‘ folder named clients 
            Set objFolder = objNS.Folders.Item("brochure@me.co.uk").Folders("Clients")
    
    ' ===============
    ' Get file ready -
    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))
     
    ‘ generate random file number 
    ' add the current date to the filename
    sName = Format(Now(), "yyyymmdd")
    
     'random number avoids file exists error
       intHigh = 10000
       intLow = 1
     Randomize
       intNumber = Int((intHigh - intLow + 1) * Rnd + intLow)
    
    ‘ContactsFolderLocation= constant holding output folder location
    MyFileLocation = ContactsFolderLocation
    If Right(MyFileLocation, 1) <> "\" Then
        MyFileLocation = MyFileLocation & "\"
    End If
    
      strFile = MyFileLocation & sName & "- Contacts " & intNumber & ".csv"
      
    
      Set objFile = objFS.CreateTextFile(strFile, False)
      If objFile Is Nothing Then
        MsgBox "Error creating export contacts file '" & strFile & "'.", vbOKOnly + vbExclamation _
          , "Invalid File"
        Exit Sub
      End If
      
      ' headings to be inserted into csv file
       With objFile
        .Write "Company" & "," & "Email1 Address" & "," & "Email1 Display Name" & "," & "Full Name" & "," & "Arable"
        .Write vbCrLf
     End With
    ' ==================
    
    Set olItems = objFolder.Items
    
     For Each olContact In olItems
    
            MyType = olContact.MessageClass
                
                ' actual field data
                    With objFile
    ‘ will not work when custom field names Arable included
                        '.Write olContact.CompanyName & "," & olContact.Email1Address & "," & olContact.Email1DisplayName & "," & olContact.FullName & "," & olContact.Arable
    
    ‘ works fine when no custom field required
                        .Write olContact.CompanyName & "," & olContact.Email1Address & "," & olContact.Email1DisplayName & "," & olContact.FullName
    
                       .Write vbCrLf
                    End With
                    
        MyCount = MyCount + 1
       
    Nextone:
    Next
    
    
        objFile.Close
       
        Set objNS = Nothing
        Set objFolder = Nothing
        Set olItems = Nothing
        Set olContact = Nothing
    
        Set objFS = Nothing
        Set objFile = Nothing
        
    Exit Sub
    
    
    ErrorHandling:
    ' error occurs when contact entry is a distribution list
    If Err.Number = 13 Then
        Resume Nextone
    Else
        MsgBox Err.Number & " " & Err.Description
    End If
       
    End Sub
    
    

    Wednesday, March 22, 2017 9:16 AM

All replies

  • Hi Keith,

    I'm moving your thread to the following dedicated Outlook for developer forum, there you should get more professional response:

    https://social.msdn.microsoft.com/Forums/en-us/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Ethan Hua


    Please remember to mark the replies as answers if they helped.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, March 23, 2017 5:18 AM
  • Thanks Ethan.
    Thursday, March 23, 2017 9:02 AM
  • Hi Keith,
    ContactItem.ItemProperties returns an ItemProperties collection that represents all standard and user-defined properties associated with the Outlook item.
    We could use ItemProperties.Item(index), where index is the name of the object or the numeric position of the item within the collection to return a single ItemProperty object.

    E.g.

    olContact.ItemProperties.Item("CustomFieldName")


    Best Regards,
    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 24, 2017 8:46 AM
    Moderator
  • You can use ContactItem.UserProperties collection to extract custom user properties.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, March 30, 2017 7:24 PM