Code:
Option Explicit
'This is our ribbon control variable
Dim iruCalendar As IRibbonUI
'And this is where we have dimensioned our module variables
'I'm just trying to be generous with the 64-bit users.
'Why check for VB 7 environment if it's already a 64 bit environment, right? Yeah, I get that all the time. Go figure.
'If you are a 'Winchester-Waco-Johnny-Dean-developer', you should look at this
'http://social.msdn.microsoft.com/Forums/office/en-US/999c5d69-a176-43e5-b5df-716f8960fc6e/my-code-if-else-for-64-bit-is-not-being-recognized
#If VBA7 Then
#If Win64 Then
Dim lngStartDay As LongPtr, _
lngEndDay As LongPtr, _
lngDayCount As LongPtr, _
lngDaySlotCount As LongPtr, _
lngSelectedYear As LongPtr, _
lngSelectedMonth As LongPtr, _
lngMonthDays(0 To 41) As LongPtr
#Else
Dim lngStartDay As Long, _
lngEndDay As Long, _
lngDayCount As Long, _
lngDaySlotCount As Long, _
lngSelectedYear As Long, _
lngSelectedMonth As Long, _
lngMonthDays(0 To 41) As Long
#End If
#Else 'Yes, it seems like a paradox, but who knows, what if it's 64 bit and still VB6
Dim lngStartDay As Long, _
lngEndDay As Long, _
lngDayCount As Long, _
lngDaySlotCount As Long, _
lngSelectedYear As Long, _
lngSelectedMonth As Long, _
lngMonthDays(0 To 41) As Long
#End If
'Callback for customUI.onLoad
Sub LoadCalendar(ribbon As IRibbonUI)
'So this is the first callback that will run for the ribbonUI, ie, the OnLoad function
'We use an IRibbonUI control to set the object
Set iruCalendar = ribbon
'We also initialize our calendar control for the current year and date (so this is what the user will see by default)
lngSelectedYear = Year(Date)
lngSelectedMonth = Month(Date)
End Sub
'Callback for galCalendar getEnabled
Sub GetEnabled(control As IRibbonControl, ByRef returnedVal)
'Of course we want our calendar to be enabled
'Having said that we could have avoided using the getEnabled feature, and just used enabled="true" in the XML
returnedVal = True
End Sub
'Callback for galCalendar getItemCount
Sub GetItemCount(control As IRibbonControl, ByRef returnedVal)
'I use 42 because that's what all the normal date calendars use. So 42 is basically 7 days * 6 rows
'The first row will be used to display week days Monday to Sunday
'The next 6 rows will be used to display the dates depending on where the first date for the corresponding month starts from
'For example, if we selected a non-leap year february, with the first day starting on a Monday, we would end up using only 4 rows
'and the remaining 1 row will be entirely blank
'Similarly, there will be months which start on a Sunday, which is the last column of our calendar control,
'effectively using at 1 or 2 columns of the last row, depending on whether there are 30 or 31 days for that month
returnedVal = 42
End Sub
'Callback for galCalendar getItemLabel
Sub GetItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
'So this is where all the action happens (well, at least most of it)
'So first of all, we need the top row to have the names of the week
'Of course it is up to the developer to decide on any algorithm to come up with the names of the weekdays
'So I've defined a constant string with the names of the weekdays (abbreviated, and delimited)
'Delimited so that I can split it using the split function, and assign to the respective column
Const strDay As String = "Su|Mo|Tu|We|Th|Fr|Sa"
'We now need to identify the weekday on which the first day of the month starts
lngStartDay = Weekday(DateSerial(lngSelectedYear, lngSelectedMonth, 1)) 'Year(Date), Month(Date), 1))
'We also need to know how many days there are in that month
lngEndDay = Day(DateSerial(lngSelectedYear, lngSelectedMonth + 1, 0)) 'DateSerial(Year(Date), Month(Date) + 1, 0))
'Now we use a select case to distinquish between the top row of our calendar, and the remaining rows
'As you know, we have 7 columns. But what we need to be aware of is that the index parameter passed by this function starts from zero (0)
'So in my select case, I used < 7 instead of <= 7
Select Case index < 7
Case True 'Of course you know what this means
'Here we just pass back the name of each of the 7 weekdays as labels (yeah that's right, labels. Isn't that what the function name suggests?).
'In other words, you can consider labels as a caption (you know, like for a commandbutton, or a userform. OK, you got the idea)
returnedVal = Split(strDay, "|")(index) 'This is where we pass the name of the weekday as a label for the control
Case Else 'Now, here's where the date part begins (because this is after the first 7 controls (or the top most row of our calendar)
'You remember we had already captured the weekday on which the first day of the month starts
'We also know how many days there are in the month
'Now we need to keep track of how many controls we are iterating through. For that I simply use a variable and increment it
'Note that the variables I am using have module scope (to know about scope of variables, visit http://support.microsoft.com/kb/141693)
lngDaySlotCount = lngDaySlotCount + 1
'So now we need to know when to start passing the days as labels
'For that I'm also using another module variable to increment the days and check if the days haven't exceeded the maximum days in that month
If lngDaySlotCount >= lngStartDay And lngDayCount < lngEndDay Then
lngDayCount = lngDayCount + 1 'This is the day increment variable
lngMonthDays(index) = lngDayCount 'This is an array of 42 items (0 to 41) where I keep track of the current months days. Will explain why I used this where I am using this
returnedVal = lngDayCount 'This is where we pass the day as a label for the control
End If
End Select
End Sub
'Callback for galCalendar onAction
Sub galleryOnAction(control As IRibbonControl, id As String, index As Integer)
'This is where we pass the value of the selected date, on to the sheet
'Of course this will only pass value to the active cell. So if you've selected a range of cells, still the value will only be passed to the active cell
'Using as On Error Resume Next statement just to ensure we don't loose the ribbon control due to unwanted errors (for example, if no workbook is active, then there wouldn't be an active cell, would there?)
On Error Resume Next
'Now, in the GetItemLabel callback that I used above, I am using an array that I use as a container for the labels of the 35 (42 - 7 top row) button items
'I had mentioned that I'll explain it's usage later. Well, this is where I am using it.
'To pass the value of the selected date, I am using the DateSerial function.
'Now, we already know (or we will know) the selected year and the selected month
'But that is not enough to pass the date. Yes, we need the day also. But we only know the index of the control that we pressed on
'But since we have the values of the 35 items in the array, exactly in order of placement on the ribbon, we just need to refer to the value using the index we get as argument to this function
'Oh and since some of the slots in the first row and the last row may be empty, we just need to check that before we actually pass the value
If lngMonthDays(index) Then
'So if lngMonthDays(index) is not zero, then it means it's a valid date for the selected month and year
'And we pass that to the active cell
ActiveCell.Value = DateSerial(lngSelectedYear, lngSelectedMonth, CInt(lngMonthDays(index)))
Else
'If lngMonthDays(index) is zero, then we just assume that the user clicked on the item by mistake and we just clear the activecell value
'Of course we could just not do anything at all. But I thought what the heck, let the user have an extra reason to not be casual
'If you want to be more empathetic (or sympathetic), just remove the line below)
ActiveCell.ClearContents
End If
End Sub
'Callback for galYear getItemLabel
Sub GetItemLabelYear(control As IRibbonControl, index As Integer, ByRef returnedVal)
'I probably am one of the most laziest person when it comes to programming.
'So I just hard coded the year selection option to a window of 100 years (50 before and after current year)
'And since index starts from 0, you can make out what the following line passes as returnedVal
returnedVal = Year(Date) - 50 + index
End Sub
'Callback for galYear getItemCount
Sub GetItemCountYear(control As IRibbonControl, ByRef returnedVal)
'OK, so this is where I tell the ribbon that it's only have 100 items in the year selection gallery
returnedVal = 50 * 2
End Sub
'Callback for galYear getLabel
Sub GetLabelYear(control As IRibbonControl, ByRef returnedVal)
'So whenever we need to pass a label (caption) to the year gallery, this is the function we use
'You will remember that we are passing the current year to lngSelectedYear when the ribbon is loaded
'But you'll also notice in one of the functions below that we are passing the user selected year also to this variable
'That's where we keep the label dynamic (look at galleryOnActionYear function)
returnedVal = lngSelectedYear
End Sub
'Callback for galMonth getLabel
Sub GetLabelMonth(control As IRibbonControl, ByRef returnedVal)
'So everybody who knows the MonthName fuction will know what this does. Those who don't just hit F1
returnedVal = MonthName(lngSelectedMonth, True)
End Sub
'Callback for galYear onAction
Sub galleryOnActionYear(control As IRibbonControl, id As String, index As Integer)
'So here's where we convert the index value we receive when this function is invoked, in to the year which the user intended to select
'Don't mind the word 'invoked'. It just meant 'called by the user by clicking on any of the 100 years'
lngSelectedYear = Year(Date) - 50 + index
'Here we do some cleaning and invalidation. Invalidating a control (or a ribbon) is like using the '.Dirty' function of a range object
'It's like asking the control to validate itself again, cause we told it to do so ;)
Call YearMonthChange
End Sub
'Callback for galMonth getItemLabel
Sub GetItemLabelMonth(control As IRibbonControl, index As Integer, ByRef returnedVal)
'You should be able to figure this one out by now. It follows the same principles as the GetItemLabelYear callback function
returnedVal = MonthName(index + 1, True)
End Sub
'Callback for galMonth getItemCount
Sub GetItemCountMonth(control As IRibbonControl, ByRef returnedVal)
'Same logic as above
returnedVal = 12
End Sub
'Callback for galMonth onAction
Sub galleryOnActionMonth(control As IRibbonControl, id As String, index As Integer)
'Index starts from 0. So if the user clicks to first item, we are supposed to get 1, not zero, so index + 1
lngSelectedMonth = index + 1
'Same logic as above
YearMonthChange
End Sub
Sub YearMonthChange()
'So we are invalidating the three controls (1)Day, (2)Year, (3)Month
iruCalendar.InvalidateControl "galCalendar"
iruCalendar.InvalidateControl "galYear"
iruCalendar.InvalidateControl "galMonth"
'We are also resetting our Day and DaySlot counters to empty
lngDayCount = Empty
lngDaySlotCount = Empty
'And since each month is different from each, we cannot hold the container constant
'It's an array, and we need to clear it using the Erase function
Erase lngMonthDays
End Sub
Bookmarks