Skip to main content

如何計算在Outlook中花費於約會或會議的時數/天數/週數?

Author: Kelly Last Modified: 2025-05-12

假設Outlook日曆中有許多約會與會議。現在,您想計算花費在這些約會和會議上的時數/天數/週數,有什麼好主意嗎?本文將介紹一個VBA來幫助您完成這項任務。

使用VBA計算花費於約會或會議的時數/天數/週數


使用VBA計算花費於約會或會議的時數/天數/週數

此方法將介紹一個VBA程式碼,用於計算在Outlook中指定的約會或會議所花費的時數或分鐘數。請按照以下步驟操作:

1. 切換到日曆文件夾,然後點擊選擇要計算花費時數的約會或會議。

2. 同時按下 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 窗口。

3. 點擊 插入 > 模組,然後將以下VBA程式碼粘貼到打開的模組窗口中。

VBA:計算在Outlook中花費於約會或會議的時數/分鐘數

Sub CountTimeSpent()
Dim oOLApp As Outlook.Application
Dim oSelection As Outlook.Selection
Dim oItem As Object
Dim iDuration As Long
Dim iTotalWork As Long
Dim iMileage As Long
Dim iResult As Integer
Dim bShowiMileage As Boolean

bShowiMileage = False

iDuration = 0
iTotalWork = 0
iMileage = 0

On Error Resume Next

    Set oOLApp = CreateObject("Outlook.Application")
Set oSelection = oOLApp.ActiveExplorer.Selection

    For Each oItem In oSelection
If oItem.Class = olAppointment Then
iDuration = iDuration + oItem.Duration
iMileage = iMileage + oItem.Mileage
ElseIf oItem.Class = olTask Then
iDuration = iDuration + oItem.ActualWork
iTotalWork = iTotalWork + oItem.TotalWork
iMileage = iMileage + oItem.Mileage
ElseIf oItem.Class = Outlook.olJournal Then
iDuration = iDuration + oItem.Duration
iMileage = iMileage + oItem.Mileage
Else
iResult = MsgBox("Please select some Calendar, Task or Journal items at first!", vbCritical, "Items Time Spent")
Exit Sub
End If
Next

Dim MsgBoxText As String
MsgBoxText = "Total time spent: " & vbNewLine & iDuration & " minutes"

If iDuration > 60 Then
MsgBoxText = MsgBoxText & HoursMsg(iDuration)
End If

If iTotalWork > 0 Then
MsgBoxText = MsgBoxText & vbNewLine & vbNewLine & "Total work recorded; " & vbNewLine & iTotalWork & " minutes"

If iTotalWork > 60 Then
MsgBoxText = MsgBoxText & HoursMsg(iTotalWork)
End If
End If

If bShowiMileage = True Then
MsgBoxText = MsgBoxText & vbNewLine & vbNewLine & "Total iMileage; " & iMileage
End If

    iResult = MsgBox(MsgBoxText, vbInformation, "Items Time spent")

ExitSub:
Set oItem = Nothing
Set oSelection = Nothing
Set oOLApp = Nothing
End Sub

Function HoursMsg(TotalMinutes As Long) As String
Dim iHours As Long
Dim iMinutes As Long
iHours = TotalMinutes \ 60
iMinutes = TotalMinutes Mod 60
HoursMsg = " (" & iHours & " Hours and " & iMinutes & " Minutes)"
End Function

4. 按下 F5 鍵或點擊 執行 按鈕來運行此VBA程式碼。

此時會彈出一個對話框,顯示選中的約會/會議所花費的時數/分鐘數。請參見截圖:

using vba to count hours/days/weeks spent on an appointment or meeting in Outlook

便簽:您可以同時選擇多個約會或會議,使用此VBA程式碼計算它們總共花費的時數/分鐘數。


相關文章

計算Outlook文件夾中的對話總數量

計算Outlook中選定郵件的附件總數量

計算Outlook中收件人、抄送和密件抄送欄位中的收件人數量

按寄件人計算Outlook中的郵件數量