You got it. The line/space formatting might not be exact since I'm
pasting from an e-mail I sent happy-jack, but here goes:
···
------------------------------------------------
First of all, check out the CDO Live website for details on how to
interface with all of the MAPI properties that correspond to calendar,
contact, message, etc. data. Here's a page that I relied upon -->
http://www.cdolive.com/cdo10.htm\. If you wrote Perl scripts that
interfaced with MAPI objects then you probably are familiar with what
is involved.
For the most part I just looked at the VB samples on the CDO Live
website and turned the syntax around so that they worked in Ruby. Below
is a sample method of a MAPI query that I created to pull contact
information from a Sales Contacts folder that's in the Exchange Public
Folders at my company. I use the last name and the last 4 digits of the
home telephone number as selection criteria. I create the Sales
Contacts folder object in Ruby using win32ole and my own mapi profile
housed in Outlook. Since this folder object creation is used in other
areas of my program I saved it as a method that can be reused.
def getSalesContacts
# create the Exchange server session
session = WIN32OLE.new('Mapi.Session')
session.logon('Greg Kujawa')
# navigate to the Sales Contacts entry underneath All Public Folders
publicFolders = String.new
recordCount = session.InfoStores.count
recordCount.times { |i|
publicFolders = session.InfoStores.Item(i+1).RootFolder.Folders if
session.InfoStores.Item(i+1).name == "Public Folders"
}
allPublicFolders = String.new
recordCount = publicFolders.count
recordCount.times { |i|
allPublicFolders = publicFolders.Item(i+1).Folders if
publicFolders.Item(i+1).name == "All Public Folders"
}
salesContacts = String.new
recordCount = allPublicFolders.count
recordCount.times { |i|
salesContacts = allPublicFolders.Item(i+1).messages if
allPublicFolders.Item(i+1).name == "Sales Contacts"
}
return salesContacts
end
def getMAPIRecordset(lastName, homePhone)
@allContacts = getSalesContacts
@recordCount = @allContacts.count
@resultSet = Array.new
lastName.gsub!(/\s+/, "")
@lastName = lastName.downcase
homePhone.gsub!(/\s+|\D+/, "")
homePhone =~ /(\d\d\d\d)$/
@homePhone = $1
@recordCount.times do |i| # iterate through the record count to find
the record based on given criteria
begin
@contactLastName = String.new
@contactLastName <<
@allContacts.Item(i+1).Fields.Item(@cdoPR_SURNAME).Value.to_s.downcase
rescue
@contactLastName = "Invalid"
end
begin
@contactHomePhone = String.new
@contactHomePhone <<
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_TELEPHONE_NUMBER).Value
rescue
@contactHomePhone = "Invalid"
end
if @contactLastName == @lastName and @contactHomePhone[-4..-1] ==
@homePhone
begin
@resultSet[0] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_DISPLAY_NAME).value.to_s
rescue
@resultSet[0] = " "
end
begin
@resultSet[1] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_COMPANY_NAME).value.to_s
rescue
@resultSet[1] = " "
end
begin
@resultSet[2] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_SURNAME).value.to_s
rescue
@resultSet[2] = " "
end
begin
@resultSet[3] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_MIDDLE_NAME).value.to_s
rescue
@resultSet[3] = " "
end
begin
@resultSet[4] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_GIVEN_NAME).value.to_s
rescue
@resultSet[4] = " "
end
begin
@resultSet[5] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_STREET_ADDRESS).value.to_s
rescue
@resultSet[5] = " "
end
begin
@resultSet[6] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_LOCALITY).value.to_s
rescue
@resultSet[6] = " "
end
begin
@resultSet[7] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_STATE_OR_PROVINCE).value.to_s
rescue
@resultSet[7] = " "
end
begin
@resultSet[8] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_POSTAL_CODE).value.to_s
rescue
@resultSet[8] = " "
end
begin
@resultSet[9] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_BODY).value.to_s
rescue
@resultSet[9] = " "
end
begin
@resultSet[10] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_CUSTOMER_NUMBER).value.to_s
rescue
@resultSet[10] = " "
end
begin
@resultSet[11] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_BIRTHDAY).value.to_s
rescue
@resultSet[11] = " "
end
begin
@resultSet[12] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_WEDDING_ANNIVERSARY).value.to_s
rescue
@resultSet[12] = " "
end
begin
@resultSet[13] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_SPOUSE_NAME).value.to_s
rescue
@resultSet[13] = " "
end
begin
@resultSet[14] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_ADDRESS_CITY).value.to_s
rescue
@resultSet[14] = " "
end
begin
@resultSet[15] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_ADDRESS_POSTAL_CODE).value.to_s
rescue
@resultSet[15] = " "
end
begin
@resultSet[16] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_ADDRESS_STATE_OR_PROVINCE).value.to_s
rescue
@resultSet[16] = " "
end
begin
@resultSet[17] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_ADDRESS_STREET).value.to_s
rescue
@resultSet[17] = " "
end
begin
@resultSet[18] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_ADDRESS_FAX_NUMBER).value.to_s
rescue
@resultSet[18] = " "
end
begin
@resultSet[19] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_HOME_TELEPHONE_NUMBER).value.to_s
rescue
@resultSet[19] = " "
end
begin
@resultSet[20] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_BUSINESS_FAX_NUMBER).value.to_s
rescue
@resultSet[20] = " "
end
begin
@resultSet[21] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_BUSINESS_TELEPHONE_NUMBER).value.to_s
rescue
@resultSet[21] = " "
end
begin
@resultSet[22] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_CELLULAR_TELEPHONE_NUMBER).value.to_s
rescue
@resultSet[22] = " "
end
begin
@resultSet[23] =
@allContacts.Item(i+1).Fields.Item(@cdoPR_ENTRYID).value.to_s
rescue
@resultSet[23] = " "
end
end
end
if @resultSet == or homePhone !~ /\d\d\d\d\d\d\d/
return 1 # failure
else
return @resultSet # success
end
end
The begin-rescue-end statements should be better organized I'm sure.
The problem seems to be cases where the contact field hasn't been
defined. MAPI throws an error message rather than simply returning a
nill/null value. The @cdo... variables are the MAPI properties that I
pulled from the CDO Live website. A more in-depth reference can be
found at
Technical documentation | Microsoft Learn.
I know my sample code doesn't directly deal with the Calendar MAPI
object but if you use my stuff as a guide and look at the CDO Live
website I'm sure you can get where you need to go. Hope this helps!