23 September 2014

How To Recreate the Birthday and Anniversary reminders in the Outlook Calendar

Here is how to recreate the Birthday and Anniversary reminders in the Outlook Calendar. Put this macro code in Outlook MVA Editor (Alt-F11) and run.

Sub olRobot()
' Outlook VBA script by Sergii Vakula
' Auto generation the Birthdays and Anniversaries appointments of all Contact folders to a specific calendar
' Auto changing Contact's FileAs fields: FullName for humans, CompanyName for companies

Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objItems As Outlook.Items
Dim obj As Object
Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")

On Error Resume Next

' *****************************************************************************************************
' *** STAGE 1: Rebuilding Contact's Birthdays and Anniversaries to the main calendar, fixing FileAs ***
' *****************************************************************************************************

Dim Report As String
Dim mySession As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Set mySession = Application.Session

' Method 1: Ask for Contact folder
'MsgBox ("Select Contact folder by next step...")
'Call ContactsFolders(Session.PickFolder, Report)

' Method 2: Use default Contact folder and all subfolders
'Call ContactsFolders(objNS.GetDefaultFolder(olFolderContacts), Report)

' Method 3: Use all Contact folders
For Each myFolder In mySession.Folders
Call ContactsFolders(myFolder, Report)

' ***************************************************************************************
' *** STAGE 2: Moving Birthdays and Anniversaries appointments to a specific calendar ***
' ***************************************************************************************

Dim objCalendar As Outlook.AppointmentItem
Dim objCalendarFolder As Outlook.MAPIFolder
Dim cAppt As AppointmentItem
Dim moveCal As AppointmentItem
Dim pattern As RecurrencePattern
Set objCalendarFolder = objNS.GetDefaultFolder(olFolderCalendar)
bodyMessage = "This is autocreated appointment"

' Method 1: Ask for specific calendar folder for birthdays and anniversaries
'MsgBox ("Select Birthdays and Anniversaries Calendar folder by next step...")
'Set newCalFolder = Session.PickFolder

' Method 2: Use pre-assigned calendar folder for birthdays and anniversaries
'Set newCalFolder = GetFolderPath("display name in folder listCalendarBirthdays and Anniversaries")
Set newCalFolder = GetFolderPath("\sv@pbxsphere.comCalendarBirthdays and Anniversaries")

For i = newCalFolder.Items.Count To 1 Step -1
Set obj = newCalFolder.Items(i)
If obj.Class = olAppointment And _
obj.GetRecurrencePattern.RecurrenceType = olRecursYearly And _
obj.AllDayEvent And _
obj.Body = bodyMessage Then
Set objCalendar = obj
End If

For i = objCalendarFolder.Items.Count To 1 Step -1
Set obj = objCalendarFolder.Items(i)
If obj.Class = olAppointment And _
obj.GetRecurrencePattern.RecurrenceType = olRecursYearly And _
obj.AllDayEvent And _
(Right(obj.Subject, 11) = "'s Birthday" Or Right(obj.Subject, 14) = "'s Anniversary" Or _
Right(obj.Subject, 13) = "???? ????????" Or Right(obj.Subject, 9) = "?????????") Then

Set objCalendar = obj
Set cAppt = Application.CreateItem(olAppointmentItem)

With cAppt
.Subject = objCalendar.Subject
.Start = objCalendar.Start
.Duration = objCalendar.Duration
.AllDayEvent = True
.Body = bodyMessage
.ReminderSet = False
.BusyStatus = olFree
End With

Set pattern = cAppt.GetRecurrencePattern
pattern.RecurrenceType = olRecursYearly


Set moveCal = cAppt.Move(newCalFolder)
'moveCal.Categories = "moved"

End If

Set objOL = Nothing
Set objNS = Nothing
Set obj = Nothing
Set objContact = Nothing
Set objItems = Nothing
Set objCalendar = Nothing
Set objCalendarFolder = Nothing
Set cAppt = Nothing
Set moveCal = Nothing
Set pattern = Nothing
Set mySession = Nothing
Set myFolder = Nothing

MsgBox ("Completed!" & vbCrLf & vbCrLf & "All Contact's FileAs were fixed." & vbCrLf & "All Birthdays and Anniversaries appointments were re-created." & vbCrLf & vbCrLf & "Contact folders that been processed:" & vbCrLf & Report & vbCrLf & "Calendar for Birhdays and Anniversaries:" & vbCrLf & newCalFolder.FolderPath & vbCrLf & vbCrLf & "Have a nice day!")

End Sub

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
FoldersArray = Split(FolderPath, "")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
End If
Set GetFolderPath = oFolder
Exit Function
Set GetFolderPath = Nothing
Exit Function
End Function

Private Sub ContactsFolders(CurrentFolder As Outlook.Folder, Report As String)
Dim objItems As Outlook.Items
Dim obj As Object
Dim objContact As Outlook.ContactItem
Dim strFileAs As String
Dim SubFolder As Outlook.Folder
Dim SubFolders As Outlook.Folders
Set SubFolders = CurrentFolder.Folders
If CurrentFolder.DefaultItemType = 2 Then
Report = Report & CurrentFolder.FolderPath & vbCrLf
Set objItems = CurrentFolder.Items
For Each obj In objItems
If obj.Class = olContact Then
Set objContact = obj

With objContact

If .FullName = "" Then
strFileAs = .CompanyName
strFileAs = .FullName
End If

.FileAs = strFileAs

mybirthday = .Birthday
myanniversary = .Anniversary
.Birthday = Now
.Anniversary = Now
.Birthday = mybirthday
.Anniversary = myanniversary

.Close 0
End With

End If

End If

For Each SubFolder In SubFolders
Call ContactsFolders(SubFolder, Report)

Set SubFolder = Nothing
Set SubFolders = Nothing

End Sub

17 September 2014

Best monetize model for Windows Phone/Store Apps

Free version with ads
paid version with try option

07 September 2014