Skip to main content

如何根據生日在 Outlook 日曆中計算並顯示年齡?

Author: Kelly Last Modified: 2025-05-12

一般來說,聯絡人的生日會自動作為每年重複的約會添加到日曆中。但您知道如何計算聯絡人的年齡並將其直接顯示在日曆視圖中嗎?本文將介紹一種 VBA 方法來計算每位聯絡人的年齡,並將其顯示在相關重複約會的主題中。

根據生日在 Outlook 日曆中計算並顯示年齡

the screenshot of calculating age by birthdays in outlook

根據生日在 Outlook 日曆中計算並顯示年齡

要計算每位聯絡人的年齡並將其顯示在 Outlook 日曆中相關重複約會的主題中,請按照以下步驟操作:

1. 打開預設的日曆文件夾,然後按下「Alt」+「F11」鍵以打開「Microsoft Visual Basic for Applications」窗口。

2. 點擊「插入」>「模組」,然後將以下 VBA 程式碼粘貼到新的模組窗口中。

VBA:在 Outlook 日曆中計算並顯示聯絡人年齡

Option Explicit
Public Sub UpdateAges()
Dim xOlApp As Outlook.Application
Dim xOlFolder As Outlook.Folder
Dim xOlItems As Outlook.Items
Dim xAppointmentItem As AppointmentItem
Dim xAge As Integer
Dim xOlProp As Outlook.UserProperty
Set xOlApp = Outlook.Application
Set xOlFolder = Session.GetDefaultFolder(olFolderCalendar)
Set xOlItems = xOlFolder.Items
For Each xAppointmentItem In xOlItems
If (InStr(1, xAppointmentItem.Subject, "Birthday") Or InStr(1, xAppointmentItem.Subject, "Anniversary")) And xAppointmentItem.IsRecurring = True Then
With xAppointmentItem
If xAppointmentItem.UserProperties("Original Subject") Is Nothing Then
Set xOlProp = xAppointmentItem.UserProperties.Add("Original Subject", olText, True)
xOlProp.Value = .Subject
.Save
End If
xAge = DateDiff("yyyy", .Start, Date)
.Subject = .UserProperties("Original Subject") & " (" & xAge & " in " & Format(Date, "yyyy") & ")"
.Save
End With
End If
Next
Set xAppointmentItem = Nothing
Set xOlItems = Nothing
Set xOlFolder = Nothing
Set xOlApp = Nothing
End Sub

3. 按下「F5」或點擊「執行」按鈕來執行 VBA。

當您返回預設日曆時,您將看到每位聯絡人的年齡已計算並顯示在重複生日約會的主題中。請參見截圖:

the screenshot of step about calculating age by birthdays in outlook 1

注意:
(1) 在聯絡人重複生日約會的主題中,聯絡人的年齡顯示在括號內,例如(2017 年 41 歲),其中 41 是年齡,2017 是當前年份。
(2) 如果您更改了聯絡人文件夾中的生日,則日曆中顯示的年齡將自動刪除。
(3) 此 VBA 僅適用於預設的 Outlook 日曆。


相關文章