Setting Outlook Appointment Label Color using code

Davers

Well-known Member
Joined
Sep 17, 2002
Messages
1,165
Does anyone know how to set the label color through code? I'm using this to import several hundred appointments into my Outlook Calendar, and I'd like to set the color of them to something other than white...can this be done with code?

Code:
Sub enterAPPT()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olAppt As Outlook.AppointmentItem
Dim Calendar As Outlook.MAPIFolder
Dim Installers As Outlook.MAPIFolder
Dim olSub, myTime, myDate, myPeeps, myLoc, myCDSID, myDept As String

For Each i In Range("C16:C" & Range("C65536").End(xlUp).Row)
If i.Value = "" Then Exit For

myCell = i.Address
myTime = Format(Range(myCell).Offset(0, 6), "hh:mm AM/PM")
myDate = Range(myCell).Offset(0, 5)
myPeeps = Range(myCell).Offset(0, 3)
myLoc = Range(myCell).Offset(0, 4)
myCDSID = Range(myCell).Offset(0, 1).Value
myDept = Range(myCell).Offset(0, 2)

Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon

Set Calendar = olNs.GetDefaultFolder(olFolderCalendar)
Set olAppt = olApp.CreateItem(olAppointmentItem)

olSub = "PC Renewal Schedule"
olAppt.Start = myTime + myDate
 
    With olAppt
        .Duration = 120
        .Subject = olSub
        .Body = myCDSID & " - " & myPeeps & " - " & olSub
        .Location = myDept & " - " & myLoc
        .ReminderMinutesBeforeStart = 30
        .ReminderSet = True
    End With
    
    olAppt.Save
Next

Set olApp = Nothing
Set olNs = Nothing
Set olAppt = Nothing
Set Calendar = Nothing

End Sub

Thanks,

Dave (y)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Okay, first, I cannot take credit for this code...it was found here...http://www.outlookcode.com/codedetail.aspx?id=139.

This will set the label color of a saved appointment item. I have adjusted it slightly so that it does not require a reference to the CDO library to be added.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> SetApptColorLabel(objAppt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, _
                      intColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)
    <SPAN style="color:#007F00">' requires reference to CDO 1.21 Library</SPAN>
    <SPAN style="color:#007F00">' adapted from sample code by Randy Byrne</SPAN>
    <SPAN style="color:#007F00">' intColor corresponds to the ordinal value of the color label</SPAN>
        <SPAN style="color:#007F00">'1=Important, 2=Business, etc.</SPAN>
    <SPAN style="color:#00007F">Const</SPAN> CdoPropSetID1 = "0220060000000000C000000000000046"
    <SPAN style="color:#00007F">Const</SPAN> CdoAppt_Colors = "0x8214"
    <SPAN style="color:#00007F">Dim</SPAN> objCDO <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> objMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> colFields <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> objField <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> intAns <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> objCDO = CreateObject("MAPI.Session")
    objCDO.Logon "", "", <SPAN style="color:#00007F">False</SPAN>, <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> objAppt.EntryID = "" <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> objMsg = objCDO.GetMessage(objAppt.EntryID, _
                                   objAppt.Parent.StoreID)
        <SPAN style="color:#00007F">Set</SPAN> colFields = objMsg.Fields
        <SPAN style="color:#00007F">Set</SPAN> objField = colFields.Item(CdoAppt_Colors, CdoPropSetID1)
        <SPAN style="color:#00007F">If</SPAN> objField <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
            Err.Clear
            <SPAN style="color:#00007F">Set</SPAN> objField = colFields.Add(CdoAppt_Colors, vbLong, intColor, CdoPropSetID1)
        <SPAN style="color:#00007F">Else</SPAN>
            objField.Value = intColor
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        objMsg.Update <SPAN style="color:#00007F">True</SPAN>, <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">Else</SPAN>
        strMsg = "You must save the appointment before you add a color label. " & _
                 "Do you want to save the appointment now?"
        intAns = MsgBox(strMsg, vbYesNo + vbDefaultButton1, "Set Appointment Color Label")
        <SPAN style="color:#00007F">If</SPAN> intAns = vbYes <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">Call</SPAN> SetApptColorLabel(objAppt, intColor)
        <SPAN style="color:#00007F">Else</SPAN>
            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                      
    <SPAN style="color:#00007F">Set</SPAN> objAppt = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> objMsg = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> colFields = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> objField = <SPAN style="color:#00007F">Nothing</SPAN>
    objCDO.Logoff
    <SPAN style="color:#00007F">Set</SPAN> objCDO = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

The way you would use this in your code is as follows...

<font face=Courier New>olSub = "PC Renewal Schedule"
olAppt.Start = myTime + myDate

    <SPAN style="color:#00007F">With</SPAN> olAppt
        .Duration = 120
        .Subject = olSub
        .Body = myCDSID & " - " & myPeeps & " - " & olSub
        .Location = myDept & " - " & myLoc
        .ReminderMinutesBeforeStart = 30
        .ReminderSet = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
    olAppt.Save

    SetApptColorLabel olAppt, 2
<SPAN style="color:#00007F">Next</SPAN></FONT>

:eek: This has been very lightly tested, but it did seem to work on my end.
 
Upvote 0
:eek: Ouch! Thanks for the reply TommyGun!!! I think I may just do this manually, I've found out I can color appointments based on the subject line, and since they all have "PC Renewal Schedule" as the subject, that will probably be easier...

And just for fun, I created 2 more subs, one deletes all of these appointments just in case it was a mistake....and the other will send them out...what a time saver!!!

Thanks again...I'll have to check out that web site!!!

Have a good day,

Dave (y)
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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