Background: I work in Finance as a planner. I'm looking to automate birthday emails to clients. Typically smaller clients, as larger get a custom hard copy mailed directly. Our current method of outreach is to manually enter them once a month in American Greetings, however, this is becoming tedious and time-consuming. I'd like to send a simple email using my contacts database in outlook.
I've been able to follow a guide that walked me through the process however, I'm making a mistake somewhere along the way.
The idea is to use visualbasics and the task system to pull up a prompt for me to send the email through reminders.
This is the guide I've been following and the code from it below: https://www.extendoffice.com/documents/outlook/5050-automatically-send-birthday-email-outlook.html#a1
Can someone point me in the right direction? Would this code run as intended? Is it outdated? I'd like to know where my problem is so I can try to correct it.
Dim xTempMail As MailItem
Dim xFilePath As String
Dim xItems As Outlook.Items
Dim xItem As Object
Dim xContactItem As Outlook.ContactItem
Dim xTodayDate As String
Dim xBirthdayDate As String
Dim xGreetingMail As Outlook.MailItem
Dim xWordDoc As Word.Document
Dim xGreetings As String
Dim xBool As Boolean
xFilePath = CreateObject("shell.Application").NameSpace(5).self.Path & "\UserTemplates"
Set xFSO = CreateObject("Scripting.FileSystemObject")
If xFSO.FolderExists(xFilePath) = False Then
MkDir xFilePath
End If
If IsFileExists(xFilePath & "\Birthday Greeting Mail.oft") = False Then
Set xTempMail = Outlook.CreateItem(olMailItem)
xTempMail.SaveAs xFilePath & "\Birthday Greeting Mail.oft", olTemplate
xTempMail.Close olDiscard
End If
If (TypeOf Item Is TaskItem) And (Item.Subject = "Send Birthday Greeting Mail") Then
xGreetings = "Happy Birthday!"
xGreetings = InputBox("Input birthday greetings", "Kutools for Outlook", xGreetings)
xTodayDate = Month(Date) & "-" & Day(Date)
Set xItems = Outlook.Application.Session.GetDefaultFolder(olFolderContacts).Items
For Each xItem In xItems
If Not (TypeOf xItem Is ContactItem) Then Exit Sub
Set xContactItem = xItem
xBirthdayDate = Month(xContactItem.Birthday) & "-" & Day(xContactItem.Birthday)
If xBirthdayDate = xTodayDate Then
Set xGreetingMail = Outlook.Application.CreateItemFromTemplate(xFilePath & "\Birthday Greeting Mail.oft")
Set xWordDoc = xGreetingMail.GetInspector.WordEditor
xWordDoc.Range.InsertBefore "Dear " & xContactItem.LastName & Chr(10) & xGreetings & Chr(10) & Chr(10)
With xGreetingMail
.Recipients.Add (xContactItem.Email1Address)
.Subject = "Happy Birthday!"
.Display
.Close (olSave)
.Send
End With
End If
Next
End If
End Sub
Function IsFileExists(ByVal FileName As String) As Boolean
Dim xFileSystem As Object
Set xFileSystem = CreateObject("Scripting.FileSystemObject")
If xFileSystem.FileExists(FileName) = True Then
IsFileExists = True
Else
IsFileExists = False
End If
End Function```
CodePudding user response:
Off the top of my head:
1 Get rid of the line
If Not (TypeOf xItem Is ContactItem) Then Exit Sub
or change it to
If Not (TypeOf xItem Is ContactItem) Then Continue
Otherwise you bail out as soon as you encounter a distribution list
2 Do not call both Display
and Send
. It is either/or
Most importantly, step through your code and see where/why it breaks.
CodePudding user response:
Nobody could help you until you find a line of code where error is thrown (if any). Or just try to set a breakpoint and then run each line of code under the debugger, so you will understand how the code works and when it doesn't.
I have noticed the following lines of code where you iterate over all items in the folder:
For Each xItem In xItems
If Not (TypeOf xItem Is ContactItem) Then Exit Sub
So, if the first item is not a contact item the rest of the loop will never run for other items. If you need to run the code only for contact items:
For Each xItem In xItems
If (TypeOf xItem Is ContactItem) Then
Set xContactItem = xItem
xBirthdayDate = Month(xContactItem.Birthday) & "-" & Day(xContactItem.Birthday)
If xBirthdayDate = xTodayDate Then
Set xGreetingMail = Outlook.Application.CreateItemFromTemplate(xFilePath & "\Birthday Greeting Mail.oft")
Set xWordDoc = xGreetingMail.GetInspector.WordEditor
xWordDoc.Range.InsertBefore "Dear " & xContactItem.LastName & Chr(10) & xGreetings & Chr(10) & Chr(10)
With xGreetingMail
.Recipients.Add (xContactItem.Email1Address)
.Subject = "Happy Birthday!"
.Display
.Close (olSave)
.Send
End With
End If
End If
Next
Be aware, an Outlook folder may contain different kind of items - mails, tasks, documents and etc.
In the code you display a newly created item and then close it back:
With xGreetingMail
.Recipients.Add (xContactItem.Email1Address)
.Subject = "Happy Birthday!"
'.Display
'.Close (olSave)
.Send
End With
There is no need to display an item if you need to send it out.