How to accept a meeting invitation and set a category in Outlook with VBA? - vba

I am using VBA to accept certain calendar invites. When the meeting invite requests a response, I'm able to accept and send the response while setting the category for the copy stored on my calendar with the following script (also discussed here).
For x = Application.ActiveWindow.Selection.Count To 1 Step -1
If (Application.ActiveWindow.Selection.Item(x).MessageClass = "IPM.Schedule.Meeting.Request") Then
Set cAppt = Application.ActiveWindow.Selection.Item(x).GetAssociatedAppointment(True)
Set oRequest = cAppt.Respond(olMeetingAccepted, True)
If cAppt.ResponseRequested = True Then
oRequest.Send
oRequest.Categories = "xxxx"
Else
cAppt.Categories = "xxxx"
End If
End If
Next x
But when the invite does not request a response, no MeetingItem is generated and setting the category on the original AppointmentItem has no effect (e.g. cAppt.Categories = "xxxx").
How do I set the calendar item category with VBA for an appointment request that does not request a response?
(updated question to actually show attempt to set category when no response is requested)

After you set the category, you need to save the appointment:
cAppt.Categories = "xxxx"
cAppt.Save

How do I set the calendar item category with VBA for an appointment request that does not request a response?
You need to set a category on the appointment item in your calendar, not a meeting request in that case.
The Categories property returns or sets a string representing the categories assigned to the Outlook item. If you don't see the category assigned to the item you need call the Save method as Dmitry suggested.
For x = Application.ActiveWindow.Selection.Count To 1 Step -1
If (Application.ActiveWindow.Selection.Item(x).MessageClass = "IPM.Schedule.Meeting.Request") Then
Set cAppt = Application.ActiveWindow.Selection.Item(x).GetAssociatedAppointment(True)
Set oRequest = cAppt.Respond(olMeetingAccepted, True)
If cAppt.ResponseRequested = True Then
oRequest.Categories = "xxxx"
oRequest.Send
Else
cAppt.Categories = "xxxx"
cAppt.Save
End If
End If
Next x

Related

Attempting to respond to an Outlook meeting request in VBA always returns "nothing"

I'm working on a simple Outlook VBA script to accept all selected meeting requests. Many online examples suggest something like the following code should work:
Sub AcceptItem()
Dim cAppt As AppointmentItem
Dim oRequest As MeetingItem
Dim x As Integer
For x = Application.ActiveWindow.Selection.Count To 1 Step -1
If (Application.ActiveWindow.Selection.Item(x).MessageClass = "IPM.Schedule.Meeting.Request") Then
Set cAppt = Application.ActiveWindow.Selection.Item(x).GetAssociatedAppointment(True)
Set oRequest = cAppt.Respond(olMeetingAccepted, True)
oRequest.Send
End If
Next x
End Sub
But the script always fails at oRequest.send -- when I inspect with the debugger, oRequest is always set to Nothing after the Respond line is executed, rather than containing a MeetingItem.
What am I doing wrong?
Before calling the Respond method in the code you need to check the AppointmentItem.ResponseRequested property which returns a boolean that indicates true if the sender would like a response to the meeting request for the appointment.
For x = Application.ActiveWindow.Selection.Count To 1 Step -1
If (Application.ActiveWindow.Selection.Item(x).MessageClass = "IPM.Schedule.Meeting.Request") Then
Set cAppt = Application.ActiveWindow.Selection.Item(x).GetAssociatedAppointment(True)
If cAppt.ResponseRequested = True Then
Set oRequest = cAppt.Respond(olMeetingAccepted, True)
oRequest.Send
End If
End If
Next x

Decline Outlook meeting based on recipient being a distribution list?

I get emails from my old team/role about tech bridges during outages that I no longer attend. I would like to auto-decline them IF they are sent to a specific distribution list (that I'm still part of).
I don't know VBA at all but would this work (I modified another script I found and replaced SenderEmailAddress with Recipients). Don't want to run this until someone who actually knows that they are looking at confirms or denies that this will work (on the off chance it does something wild with me emails).
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim xEntryIDs
Dim xItem
Dim i As Integer
Dim xMeeting As MeetingItem, xMeetingDeclined As MeetingItem
Dim xAppointmentItem As AppointmentItem
On Error Resume Next
xEntryIDs = Split(EntryIDCollection, ",")
For i = 0 To UBound(xEntryIDs)
Set xItem = Application.Session.GetItemFromID(xEntryIDs(i))
If xItem.Class = olMeetingRequest Then
Set xMeeting = xItem
xMeeting.ReminderSet = False
If VBA.LCase(xMeeting.Recipients) = VBA.LCase("support.bridge#company.com") Then
Set xAppointmentItem = xMeeting.GetAssociatedAppointment(True)
xAppointmentItem.ReminderSet = False
Set xMeetingDeclined = xAppointmentItem.Respond(olMeetingDeclined)
xMeetingDeclined.Body = "Declined"
xMeetingDeclined.Send
xMeeting.Delete
End If
End If
Next
End Sub
You can't compare the Recipients collection with a string in the following way:
If VBA.LCase(xMeeting.Recipients) = VBA.LCase("support.bridge#company.com") Then
The MeetingItem.Recipients property returns a Recipients collection that represents all the recipients for the Outlook item. Use Recipients(index), where index is the name or index number, to return a single Recipient object. The name can be a string representing the display name, the alias, or the full SMTP email address of the recipient.
The Recipient.DisplayType property returns a constant belonging to the OlDisplayType enumeration that describes the nature of the Recipient. It seems you are interested in the olDistList or olPrivateDistList values.
Also I've noticed the following line of code:
xEntryIDs = Split(EntryIDCollection, ",")
The NewMailEx behavior has been changed more than 10 years ago and now it is fired for each Outlook item separately. So, there is no need to split the string in the event handler, the parameter contains only a single entry ID value.

Script to send Outlook calendar by e-mail, change detail level?

I have a script found on https://www.crestwood.com/2018/08/17/automating-outlook-calendar-to-send-daily-agenda/. This works but I can not control the details of the calendar. Or more correctly I can't figure out how to send Meeting subject and Location.
Is there a way with this code? Or could I go about it in some other way?
When doing it manually in Outlook it is called "Limited details" - this is my desired outcome.
I can change the constant: olFreeBusyAndSubject between 0, 1, 2. But none of these return my desired outcome. They result in (only seeing busy/free, Only seeing subject, Full details including description of event). I do not want full details since their might be sensitive information which I do not want on my secondary e-mail.
' -------------------------------------------------
' Modify this \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Const myEmailAddress = "some#email.com"
Const includePrivateDetails = TRUE
Const howManyDaysToDisplay = 1
' Modify this /////////////////////////////////////
' -------------------------------------------------
Const olCalendarMailFormatDailySchedule = 0
Const olFreeBusyAndSubject = 2
' Const olFullDetails = 1
Const olFolderCalendar = 9
SendCalendar myEmailAddress, Date, (Date + (howManyDaysToDisplay - 1))
Sub SendCalendar(strAdr, datBeg, datEnd)
Dim olkApp, olkSes, olkCal, olkExp, olkMsg
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = OlkApp.GetNameSpace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkCal = olkSes.GetDefaultFolder(olFolderCalendar)
Set olkExp = olkCal.GetCalendarExporter
With olkExp
.CalendarDetail = olFreeBusyAndSubject
.IncludePrivateDetails = includePrivateDetails
.RestrictToWorkingHours = False
.StartDate = datBeg
.EndDate = datEnd
End With
Set olkMsg = olkExp.ForwardAsICal(olCalendarMailFormatDailySchedule)
With olkMsg
.To = strAdr
.Send
End With
Set olkCal = Nothing
Set olkExp = Nothing
Set olkMsg = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing
End Sub
As described desired outcome is to thorugh a VBA-script (or other automated way) send "Limited details"-Outlook-calendar by e-mail.
Thank you in advance!
In .CalendarDetail = olFreeBusyAndSubject replace olFreeBusyAndSubject by olFullDetails.
To discover this I used Bing and searched for "Outlook CalendarDetail". I find this an easy method for discovering the permitted values for a property.

QTP, send mailer address

I am sending an email using the QTP outlook object model.
Here is the piece of code.
'Create an object of type Outlook
Set objOutlook = CreateObject("Outlook.Application")
Set myMail = objOutlook.CreateItem(0)
'Set the email properties
myMail.To = "some_mail_id#gmail.com"
myMail.CC = "some_mail_id_2#gmail.com; some_other_mail#yahoo.com" 'Sending mails to multiple ids
myMail.BCC = "" 'If BCC is not required, then this line can be omitted
myMail.Subject = "Sending mail from MS Outlook using QTP"
myMail.Body= "Test Mail Contents"
myMail.Attachments.Add("D:\Attachment.txt") 'Path of the file to be attached
'Send the mail
myMail.Send
Now I needed to retrieve the sender email address & store it in an environment variable. myMail.Sender or myMail.sendermailaddres both of them are not working me.
The following code will give you the first email address the user you're connected to Outlook has access to:
objOutlook.Session.Accounts.Item(0)
I use a loop to find the account I want to send from like this:
iAccount = 0
For iLoop = 1 To oOutlook.Session.Accounts.Count
If UCase(Trim(oOutlook.Session.Accounts.Item(iLoop))) = UCase(Trim(EmailData("SendFrom"))) Then
iAccount = iLoop
Exit For
End If
Next
where EmailData is a Dictionary object containing the items I'm using for the mail item. When creating the mail item I use Set oMailItem.SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount) to specify the account it should be sent from.

Outlook not displaying the latest email while automating

I am using the following code to retrieve and check an email, but outlook is returning the mail starting from 12/22, which is neither the latest nor the oldest, while on a co worker's machine its picking up the oldest mail.
Set oapp = CreateObject("Outlook.Application")
Set oMAPI = oapp.GetNamespace("MAPI")
Set oInbox = oMAPI.GetDefaultFolder(6)
oInbox.Display
Set oallmails = oInbox.Items
Set oreqemail = oallmails.GetFirst
For oTotalmail = 1 To oallmails.Count
ostringmatch = oreqemail.Subject
'Using regex function to match
'If MatchString(ostringmatch,"89554 Completed") Then
'End If
'Exit For
Set oreqemail = oallmails.GetNext
Next
Am I missing any outlook setting, as the code looks ok to me.
Thanks
To be sure that you get always the latest or oldest email in Outlook you need to use the Sort method of the Items class. It sorts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method. The name of the property by which to sort, which may be enclosed in brackets, for example, "[CompanyName]".
Note, Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view.
Set oapp = CreateObject("Outlook.Application")
Set oMAPI = oapp.GetNamespace("MAPI")
Set oInbox = oMAPI.GetDefaultFolder(6)
oInbox.Display
Set oallmails = oInbox.Items
oallmails.Sort "[RecievedTime]"
Set oreqemail = oallmails.GetFirst
For oTotalmail = 1 To oallmails.Count
ostringmatch = oreqemail.Subject
'Using regex function to match
'If MatchString(ostringmatch,"89554 Completed") Then
'End If
'Exit For
Set oreqemail = oallmails.GetNext
Next
See Outlook VBA: How to sort emails by date and open the latest email found? for more information.