vba to send calendar appointments rather then emails

Tej92

Board Regular
Joined
Sep 27, 2022
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hello all, I'm trying to get this code to send appointments with the task to carry out but having trouble, tried multiple solutions from the web, I managed to send them but it was requiring outlook to be open but i'd like to use CDO to send them as i did for the emails.
Currently it sends the week number as when to do the task by looking at the value selected in b12, now i want it to calculate the dates by getting the week number and send the invite from Monday to Friday of that week and it should make this invite a non-busy appointment.
the body of the current message should be the body of the appointment too.

Private Sub CommandButton1_Click()
Dim Pass As String
Pass = "1234"

If InputBox("Please enter the password to run the macro") <> Pass Then
MsgBox "Incorrect password!", vbExclamation, "Error"
Exit Sub
End If

Dim objMail As Object
Set objMail = CreateObject("CDO.Message")

With objMail.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "xxxxsmtpxx.server.xxxx.xx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With

Dim weekNumber As Variant
weekNumber = Range("B12").Value

Dim weekColumn As Long
weekColumn = 0
For i = 2 To 35
If Cells(13, i).Value = weekNumber Then
weekColumn = i
Exit For
End If
Next i

If weekColumn = 0 Then
MsgBox "Week " & weekNumber & " not found in range B13:AI13.", vbCritical, "Error"
Exit Sub
End If

Dim recipientRange As Range
Set recipientRange = Range(Cells(14, weekColumn), Cells(Rows.Count, weekColumn).End(xlUp))

Dim recipient As Range
Dim totalRecipients As Long
totalRecipients = recipientRange.Cells.Count

Dim sentCount As Long
sentCount = 0

Dim progressBar As String
Dim progressPercentage As Double
Dim progressLength As Integer
progressLength = 30 ' Length of the progress bar

For Each recipient In recipientRange
If recipient.Value <> "" Then
Dim areaName As String
areaName = Range("A" & recipient.Row).Value

Dim recipientEmail As String
recipientEmail = Application.WorksheetFunction.VLookup(recipient.Value, Range("AV:AW"), 2, False)

objMail.To = recipientEmail
objMail.From = "noreply@xxxxxx.co.uk"
objMail.Subject = "task Reminder for " & areaName
objMail.TextBody = Sheet9.Range("message_body").Value & Sheet9.Range("B12").Value & " in the " & areaName & " area." & vbNewLine & Sheet9.Range("message_body2").Value
objMail.Send

sentCount = sentCount + 1
progressPercentage = sentCount / totalRecipients
progressBar = String(progressLength, "=") & " " & Format(progressPercentage, "0%")

Application.StatusBar = "Sending Emails: " & progressBar
DoEvents ' Allow the status bar to update

Else
MsgBox "Empty cell found in recipient list. Stopping email sending process.", vbCritical, "Error"
Set objMail = Nothing ' Release the email object before exiting the subroutine
Application.StatusBar = False ' Clear the status bar
Exit Sub ' Exit the subroutine when an empty cell is found
End If
Next recipient

Application.StatusBar = False ' Clear the status bar
Set objMail = Nothing

MsgBox "task reminder email sent for week " & weekNumber & ".", vbInformation, "Success"
End Sub



Any help will be greatly appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top