AddressBook Object for Windows? Outlook Import/Export?

We have the AddressBook Object is for Mac OS X Contacts. Is there any counterpart for Windows Mail and/or Outlook Address Book? Or does anybody know how to access Outlook Address Books from Xojo? I would try to get info via ODBC but I am not sure if there are other more handy ways to import or export Contacts on Windows.

I’ve used Visual Basic Scripting with Xojo to read/write outlook calendar info. I imagine the same can be done for the Address Book.

Mmmm do you know a link or any online docs? Or do you have a VB Script sample for this I could start with? At least any script writing a textfile I could re-read and import in my xojo app would help. Otherwise an seamless export/import would be perfect.

Disclaimer: This is hacked by a novice in this area. I’d love to see an easier way.

I also noticed this uses MBS but I know I’ve done it without in the past

This is what I have used for getting user appointments from the exchange server.
This script is a constant in my app (kGetCalendarAppts)
USERNAME, STARTDATE & ENDDATE are replaced prior to running. (see below)

[code]Call subOutlookAppointments() 'Displays Outlook Tasks with a due date of today or before

Sub subOutlookappointments

    Dim objOutlook
    Dim objNameSpace
    Dim objFolder
    Dim MyItems
    Dim CurrentItem
    Dim CurrentAppointment
    Dim fso, MyFile
    Dim myRecipient

    Const olFolderCalendar = 9

    'Create Outlook, Namespace, Folder Objects and Task Item
    Set objOutlook = CreateObject("Outlook.application")
    Set objNameSpace = objOutlook.GetNameSpace("MAPI")
    Set objFolder = objNameSpace.GetDefaultFolder(olFolderCalendar)
    Set myRecipient = objNameSpace.CreateRecipient("USERNAME") ' USERNAME
    Set SharedFolder = objNameSpace.GetSharedDefaultFolder(myRecipient,olFolderCalendar)
    Set MyItems = SharedFolder.Items
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile= fso.CreateTextFile("C:\\Users\\pfargo\\Desktop\\testfile.txt", True)
    dtStart = CDate(#STARTDATE#) ' STARTDATE
    dtEnd = CDate(#ENDDATE#) ' ENDDATE
    myItems.IncludeRecurrences = True
    myItems.Sort "[Start]"
    For Each CurrentItem In MyItems
        Set CurrentAppointment = CurrentItem
        If CurrentAppointment.Start < dtStart Then
        ElseIf CurrentAppointment.Start >= dtStart And CurrentAppointment.Start <= dtEnd Then
           strOutput = strOutput & "<blockquote><b>Notes: </b>" & CurrentAppointment.body & "</blockquote>" & vbCrLF & vbCrLf
           MyFile.WriteLine("<subject>" & CurrentAppointment.subject) & "</subject>"
           MyFile.WriteLine("<start>" & CurrentAppointment.start) & "</start>"
           MyFile.WriteLine("<duration>" & CurrentAppointment.Duration) & "</duration>"
           MyFile.WriteLine("<location>" & CurrentAppointment.location) & "</location>"
           MyFile.WriteLine("<body>" & CurrentAppointment.body) & "</body>"
        ElseIf CurrentAppointment.Start > dtEnd Then
            Exit For
        End If  

	'Clean up

    Set objFolder = Nothing
    Set objNameSpace = Nothing
    Set objOutlook = Nothing

End Sub[/code]

The calling routine

[code]Sub GrabUserSchedule(UserName As String, dateStart As Date, dateEnd As Date)
ws = New WindowsScriptMBS

ws.Language = “VBScript”

Dim s As String = kGetCalendarAppts

s = Replace(s,“USERNAME”,UserName)
s = Replace(s,“STARTDATE”, dateStart.SQLDate)
s = Replace(s,“ENDDATE”, dateEnd.SQLDate)

ws.ExecuteStatement s

Dim outlookFile As FolderItem = SpecialFolder.Desktop.Child(“TestFile.txt”)

if outlookFile <> nil and outlookFile.Exists then


end if

End Sub

Hopefully you can get some ideas from this. There is tons of info online but like other things - finding what you need is often a frustrating process.

If you don’t have MBS I believe I used to just write the script to a text file (.vbs) and then launch it using folderitem.launch (see next post)

This is the same idea manually launching it. This script is for launching FileMaker Scripts.

You’d most likely want to use the temp folder rather than the desktop for creating the files!

#If TargetWin32 Then

Dim f As FolderItem
Dim t As TextOutputStream

f = SpecialFolder.Desktop.Child("gotolead.vbs")
t = TextOutputStream.Create(f)

t.WriteLine "Option Explicit"
t.WriteLine "Dim fmApp, fmDocs, fmDoc, SH"
t.WriteLine "Dim theFile, theScript"
t.WriteLine "theFile = ""Leads"""
t.WriteLine "theScript = ""GotoLeadFromRB"""
' hook into FileMaker
t.WriteLine "Set fmApp = CreateObject(""FMPro.Application"")"
t.WriteLine "fmApp.Visible = True"
' bring FileMaker to the front
t.WriteLine "Set SH = CreateObject("""")"
t.WriteLine "SH.AppActivate FMapp.Caption, True"
' get the collection of open files
t.WriteLine "Set fmDocs = fmApp.Documents"
' go find our target file
t.WriteLine "For Each fmDoc In fmDocs"
t.WriteLine "If InStr(LCase(fmDoc.fullname), LCase(thefile)) > 0 Then"
' this is our file, run the script
t.WriteLine "fmDoc.dofmscript (thescript)"
t.WriteLine "End If"
t.WriteLine "Next"
' clean up
t.WriteLine "Set SH = Nothing"
t.WriteLine "Set fmDoc = Nothing"
t.WriteLine "Set fmDocs = Nothing"
t.WriteLine "Set fmApp = Nothing"


Wow looks good, I will give it a try and will report later on this, thanks!

You can also use the Xojo OLEObject class. Except for “NameSpace” as Xojo’s Namespace (Bug and is reported) is in conflict with “Outlook.NameSpace”

From Microsoft’s link: you can get an idea how to convert it to Xojo.

I made some small examples just to give you an idea how you can use the OLEObject class:

Count Folders in Outlook 2010:

  Dim myOlApp As OLEObject
  Dim myNameSpace As OLEObject
  Dim myFolder As OLEObject
  Dim myEnums as New Dictionary
  Dim Count, nEnums As Integer
  Dim outText As String
  myEnums.Value("olFolderInbox") = 6
  myEnums.Value("olFolderOutbox") = 4
  myEnums.Value("olFolderContacts") = 10
  myEnums.Value("olFolderSentMail") = 5
  myEnums.Value("olFolderJunk") = 23
  // Enumeration See:
  myOlApp = New OLEObject("Outlook.Application")
  myNameSpace = myOlApp.GetNameSpace("MAPI")
  For nEnums = 0 to myEnums.Count -1 
    myFolder = myNameSpace.GetDefaultFolder(myEnums.Value(myEnums.key(nEnums)))
    Count = myFolder.Items.count
    outText = outText +myFolder.invoke("Name")+":  "+str(count)+EndOfLine.Windows

  myOlApp = Nil 

  exception err as oleexception
    msgbox err.message

Sent an email with use of Outlook:

  Dim ToAddress As String
  Dim MessageSubject As String
  Dim MessageBody As String
  //Dim MessageAttachment
  Dim ol, ns, olMailItem, myRecipient As OLEObject
  Dim newMail As OLEOBJECT
  ToAddress = ""   ' change this...
  MessageSubject = "How to use Outlook with Xojo"
  MessageBody = "*BODY* email via MAPI *BODY*"
  ol = New OLEObject ("Outlook.Application")
  ns = ol.getNamespace("MAPI")
  ns.logon "","",true,false
  newMail = ol.CreateItem(olMailItem)
  newMail.Subject = MessageSubject
  newMail.Body = MessageBody + EndOfLine
  '' validate the recipient, just in case...
  myRecipient = ns.CreateRecipient(ToAddress)
  If Not myRecipient.Resolved Then
    MsgBox "unknown recipient"
  End If
  ol = Nil
exception err as oleexception
  msgbox err.message

Open outlook Mail Item:

  Dim myOlApp As OLEObject
  Dim myItem As OLEObject
  Dim olMailItem As OLEObject
  myOlApp = New OLEObject("Outlook.Application")
  myItem = myOlApp.CreateItem(olMailItem)


Thank you John or sharing. The OLEObject looks like a much better approach than mine. Time to dive back into MS reference materials. (yuck)

[quote=37991:@John Hansen]Open outlook Mail Item:

Dim myOlApp As OLEObject
Dim myItem As OLEObject
Dim olMailItem As OLEObject

myOlApp = New OLEObject(“Outlook.Application”)
myItem = myOlApp.CreateItem(olMailItem)


I just noticed a typo in the above example. The above example works but “olMailItem” is an enumeration.

Should be like this:
Open outlook Mail Item

  Dim myOlApp As OLEObject
  Dim myItem As OLEObject
  // OlItemType Enumerations
  const olMailItem = 0
  const olContactItem = 2
  myOlApp = New OLEObject("Outlook.Application")
  myItem = myOlApp.CreateItem(olMailItem)
  myOlApp = Nil
    exception err as oleexception
    msgbox err.message

I just had a little bit of time to make another example how to get the entries from the Outlook 2010 Contact folder.
Not sure if it works for earlier versions of Outlook.

Display contact entries

  Dim myOlApp As OLEObject
  Dim myNamespace As OLEObject
  Dim myContacts As OLEObject
  Dim myItems As OLEObject
  Dim myItem As OLEObject
  Dim nItems As Integer
  Dim outText As String
  // Enumeration See:
  Const olFolderContacts = 10
  myOlApp = NEW OLEObject("Outlook.Application")
  myNamespace = myOlApp.GetNamespace("MAPI")
  myContacts = myNameSpace.GetDefaultFolder(olFolderContacts)
  myItems = myContacts.Items
  nItems = myItems.count
  outText = "Total number of contacts: " + str(nItems) + EndOfLine.Windows + EndOfLine.Windows
  If nItems > 5 Then nItems = 5   // in this case we only want to show max 5 contacts
  For i as integer = 1 to nItems 
    myItem = myContacts.Items(i)
    outText = outText + "Full Name: "+ myItem.FullName + EndOfLine.Windows _
    + "Company:  " + myItem.CompanyName + EndOfLine.Windows _
    + "Job Title:  " + myItem.JobTitle + EndOfLine.Windows _
    + "E-mail:  " +myItem.Email1Address + EndOfLine.Windows _
    + "Display as:  " + myItem.Email1DisplayName + EndOfLine.Windows  + EndOfLine.Windows
    // Contact Item info:
  Msgbox outText
  myOlApp = Nil
exception err as oleexception
  msgbox err.message

John, great! This works! Will check this out how this behaves in Outlook 2003 and Exchange Server Environment on Monday.