Creating message if Contact's birthday is today

D

devstuff

I've put together a macro to scan a users contacts folder for any
contact who's birthday is today, and create a new message to send.
I'll eventually expand it to add custom messages (based on contacts
name) and auto send the email, but for now I'm trying to establish the
basic find and create. I'm running into an 'expected object' that I
believe is a problem with the date.
Also, the way the code is written, it seems like my 'No Birthdays
Today' message would be written for every contact who's birthday isn't
today, but it's the only way I could put it together so that the
debugger didn't say 'For with no Next' or 'Next with no For'.
Any help steering me towards funtionality would be greatly
appreciated.
Code's Below.
Thanks.

Sub getContacts()
Dim today As Date
Dim olns As Outlook.NameSpace
Dim oConItems As Outlook.Items
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set oConItems = olns.GetDefaultFolder(olFolderContacts).Items

For Each oCurItem In oConItems
If oCurItem.Birthday = today Then
Dim msg As Outlook.MailItem
Set msg = Application.CreateItem(olMailItem)
msg.Subject = "Happy Birthday"
msg.Address = objContactItem.Email1Address
msg.Display
Set msg = Nothing
Else
Wscript.Echo "No Birthdays Today"
End If
Next
End Sub
 
M

Michael Bauer [MVP - Outlook]

WScript is unknown. Replace WSCript.Echo by MsgBox.

It the code runs within Outlook then delete the Set ol = New ... line, and
replace ol.GetNameSpace by Application.GetNamespace.

You do not set any date to the today variable, so the value is 0. The Date
function would return the current date.

If you want one message be displayed if there's no birthday today at all
then use another variable declared As Boolean. If one birthday is found then
set it = True. Eventually, if the variable is still False (inital value)
then you know that it never was set to True, i.e. no birthday.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Organize eMails:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Tue, 07 Aug 2007 11:13:01 -0700 schrieb (e-mail address removed):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top