Reputation: 81
The below creates an appointment in Outlook from my Excel data - is there any way to create multiple appointments, rather than what the below does which just updates a single appointment? I'd need 3 different appointments (dates for each in column number 33, 38 and 43), the code I have just makes a single appointment and updates until the last date.
Sub ResolveName()
Dim OL As Outlook.Application, ES As Worksheet, _
r As Long, i As Long, wb As ThisWorkbook
Set wb = ThisWorkbook
Set ES = wb.Sheets("Licences")
Set OL = New Outlook.Application
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.Namespace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.MAPIFolder
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Dim SharedMailboxEmail As String
SharedMailboxEmail = "[email protected]"
Set outSharedName = myNamespace.CreateRecipient(SharedMailboxEmail)
Set outCalendarFolder = myNamespace.GetSharedDefaultFolder(outSharedName, olFolderCalendar)
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
Set myRecipient = myNamespace.CreateRecipient("DTS Streetworks")
myRecipient.Resolve
r = Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To r
With Cells(i, 5)
If myRecipient.Resolved And .Value = "TTRO" And Cells(i, 6) <> "" Then
With outappointment
.Subject = "Send Notice of Intent - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 33) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
With outappointment
.Subject = "Send Notice of Making - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 38) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
With outappointment
.Subject = "Send Full Order - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 43) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
End If
End With
Next i
Set OL = Nothing
Set wb = Nothing
Set ES = Nothing
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.MAPIFolder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
UPDATE -
As per comment stating new requirements, code below:
Sub ResolveNameTTRO()
Dim OL As Outlook.Application, ES As Worksheet, r As Long, i As Long, wb As ThisWorkbook
Set wb = ThisWorkbook
Set ES = wb.Sheets("Licences")
Set OL = New Outlook.Application
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.Namespace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.MAPIFolder
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Dim SharedMailboxEmail As String
SharedMailboxEmail = "[email protected]"
Set outSharedName = myNamespace.CreateRecipient(SharedMailboxEmail)
Set outCalendarFolder = myNamespace.GetSharedDefaultFolder(outSharedName, olFolderCalendar)
Set myRecipient = myNamespace.CreateRecipient("DTS Streetworks")
myRecipient.Resolve
r = Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To r
With Cells(i, 5)
If myRecipient.Resolved And .Value = "TTRO" And Cells(i, 6) <> "" Then
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Notice of Intent - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 33) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Notice of Making - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 38) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Full Order - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 43) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
If myRecipient.Resolved And .Value = "Section 50" And Cells(i, 6) <> "" Then
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send licence - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 54) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = "Send licence to " + ES.Cells(i, 10).Value
.Save
End With
If myRecipient.Resolved And .Value = "Mobile Plant" And Cells(i, 6) <> "" Then
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send licence - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 54) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = "Send licence to " + ES.Cells(i, 10).Value
.Save
End With
End If
End With
Next i
Set OL = Nothing
Set wb = Nothing
Set ES = Nothing
End Sub
Upvotes: 1
Views: 87
Reputation: 49395
For each time specified in the Excel row you just need to repeat the following call:
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
Upvotes: 0
Reputation: 8868
Since you need to create 3 appointments, you need to move Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
inside the loop and do it 3 times. The revised code illustrates this idea.
Sub ResolveName()
Dim OL As Outlook.Application, ES As Worksheet, r As Long, i As Long, wb As ThisWorkbook
Set wb = ThisWorkbook
Set ES = wb.Sheets("Licences")
Set OL = New Outlook.Application
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.Namespace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.MAPIFolder
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Dim SharedMailboxEmail As String
SharedMailboxEmail = "[email protected]"
Set outSharedName = myNamespace.CreateRecipient(SharedMailboxEmail)
Set outCalendarFolder = myNamespace.GetSharedDefaultFolder(outSharedName, olFolderCalendar)
Set myRecipient = myNamespace.CreateRecipient("DTS Streetworks")
myRecipient.Resolve
r = Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To r
With Cells(i, 5)
If myRecipient.Resolved And .Value = "TTRO" And Cells(i, 6) <> "" Then
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Notice of Intent - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 33) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Notice of Making - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 38) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
Set outappointment = outCalendarFolder.Items.Add(olAppointmentItem)
With outappointment
.Subject = "Send Full Order - " + ES.Cells(i, 4).Value + " " + ES.Cells(i, 12).Value
.Start = ES.Cells(i, 43) + TimeValue("09:00:00")
.ReminderSet = True
.ReminderMinutesBeforeStart = 60
.Body = ES.Cells(i, 5).Value
.Save
End With
End If
End With
Next i
Set OL = Nothing
Set wb = Nothing
Set ES = Nothing
End Sub
Upvotes: 1