PDA

View Full Version : VBA Send Recurring Task In Email



hobbiton73
11-14-2013, 12:19 AM
Hi, I wonder whether someone may be able to help me please.

Using this link http://www.slipstick.com/developer/code-samples/create-task-sending-message/
I've been able to put together the folllowing script which allows me to send a task via an email to chosen recipients.


Public WithEvents myOlApp As Outlook.Application
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.Application")
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim intRes As Integer
Dim strMsg As String
Dim objTask As TaskItem
Set objTask = Application.CreateItem(olTaskItem)
Dim strRecip As String
strMsg = "Do you want to create a task for this message?"
intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "Create Task")

If intRes = vbNo Then
Cancel = False
Else
For Each recipient In Item.Recipients
strRecip = strRecip & vbCrLf & recipient.Address
Next recipient
With objTask
.Body = strRecip & vbCrLf & Item.Body
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.ReminderSet = True
.ReminderTime = DateSerial(Year(Now), Month(Now), Day(Now)) + #7:23:00 AM#
.Save
End With
Cancel = False
End If
Set objTask = Nothing
End Sub



What I'm now trying to do is amend this slightly so that the task is 'recurring' rather than a 'one off', creating a new task for every Friday at 1.00pm.

I've spent a number of days trying to find a tutorial or solution for this, but I've been unable to do so.

I just wondered whether someone could possibly look at this please and offer some guidance on how I may go about achieiving this.

Many thanks and kind regards

skatonni
11-14-2013, 06:26 AM
This code sets up a task with a moving reminder to generate an email. Change the code to send weekly rather than daily.

http://www.vboffice.net/sample.html?lang=en&mnu=2&smp=10&cmd=showitem

hobbiton73
11-14-2013, 09:14 AM
Hi @skatonni, thank you very much for taking the time to reply to my post and for the link. It's exactly what I was after.

All the best and kind regards

hobbiton73
11-14-2013, 11:59 PM
Hi @skatonni, I hope you are well.

I just wanted to give you an update on the code you kindly provided the link to.

Unfortumnately, this isn't what I was looking for. What I'm trying to do is send the email once, with the recurring task set up in the recipients 'Task' list.

Many thanks and kind regards

skatonni
11-16-2013, 11:05 AM
For anyone with this question, rather than the kludgy two-part solution suggested, hobbiton73 has this. http://www.excelforum.com/outlook-programming-vba-macros/968658-vba-recurring-task.html


Public WithEvents myOlApp As Outlook.Application

Private Sub Application_Startup()
Initialize_handler
End Sub

Public Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.Application")
End Sub

Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim intRes As Integer
Dim strMsg As String
Dim objTask As TaskItem
Set objTask = Application.CreateItem(olTaskItem)
Dim strRecip As String

strMsg = "Do you want to create a task for this message?"
intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "Create Task")

If intRes = vbNo Then
Cancel = False
Else

For Each recipient In Item.Recipients
strRecip = strRecip & vbCrLf & recipient.Address
Next recipient

With objTask
Set recpattern = .GetRecurrencePattern
recpattern.RecurrenceType = olRecursWeekly
recpattern.DayOfWeekMask = olMonday
recpattern.PatternStartDate = #11/18/2013 9:00:00 AM#
recpattern.Interval = 1
.Body = strRecip & vbCrLf & Item.Body
.Subject = Item.Subject
.Save
End With

Cancel = False

End If

Set objTask = Nothing
End Sub