Sub Export_OutboundEngine_Contacts()
'SelfCert Instructions: http://www.howto-outlook.com/howto/selfcert.htm
Dim ContactsFolder As Outlook.Folder
Dim ContactItems As Outlook.Items
Dim ResItems As Outlook.Items
Dim sFilter, strCategory As String
Dim iNumRestricted As Integer
Dim Item As ContactItem
Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
Dim strFile As String
Dim sName As String
' Use the default folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
' Use the selected folder
'Set ContactsFolder = Application.ActiveExplorer.CurrentFolder
' ===============
' Get file ready -
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
' add the current date to the filename
sName = "OutboundEngine Contacts - " & Format(Now(), "dd-mm-yyyy")
'random number avoids file exists error
intHigh = 10000
intLow = 1
Randomize
intNumber = Int((intHigh - intLow + 1) * Rnd + intLow)
' The folder path you use needs to exist
strFile = enviro & "\Desktop\" & sName & " - " & ContactsFolder & intNumber & ".csv"
Set objFile = objFS.CreateTextFile(strFile, False)
If objFile Is Nothing Then
MsgBox "Error creating file '" & strFile & "'.", vbOKOnly + vbExclamation _
, "Invalid File"
Exit Sub
End If
With objFile
.Write "email" & "," & "fname" & ","
.Write "lname" & "," & "company" & ","
.Write "telephone" & ","
.Write "bday" & "," & "bmonth" & "," & "byear" & ","
.Write vbCrLf
End With
' ==================
' Get all of the items in the folder
Set ContactItems = ContactsFolder.Items
'create the Restrict filter
strCategory = "OutboundEngine"
sFilter = "[Categories] = " & strCategory
' Apply the filter to the collection
Set ResItems = ContactItems.Restrict(sFilter)
iNumRestricted = 0
'Loop through the items in the collection.
For Each Item In ResItems
iNumRestricted = iNumRestricted + 1
With objFile
If Not Item.Email1Address = "" Then
.Write Item.Email1Address & ","
.Write Item.FirstName & ","
.Write Item.LastName & ","
.Write Item.CompanyName & ","
.Write Item.BusinessTelephoneNumber & ","
'.Write Format(Item.Birthday, "mm") & "," & Format(Item.Birthday, "dd") & "," & Format(Item.Birthday, "yy") & ","
If Item.Birthday < "01/01/2100" Then
.Write Format(Item.Birthday, "mm") & "," & Format(Item.Birthday, "dd") & "," & Format(Item.Birthday, "yy") & ","
Else
.Write ",,,"
End If
.Write vbCrLf
Else
.Write vbCrLf
End If
End With
Next
objFile.Close
MsgBox "Exported " & iNumRestricted & " contacts.", vbOKOnly + vbInformation, "DONE!"
Set objFS = Nothing
Set objFile = Nothing
Set Item = Nothing
Set ResItems = Nothing
Set ContactItems = Nothing
Set ContactsFolder = Nothing
End Sub