情シス担当の備忘録

VBA・労働法とか。

【OutlookVBA】受信したメールの受信年度を作成する

sub アイテムの受信年度を作成するマクロ()

Dim objMailItem As Object

'MailItemオブジェクトの取得
If TypeName(Application.ActiveWindow) = "Inspector" Then
  Set objMailItem= Application.ActiveInspector.CurrentItem
Else
  Set objMailItem = ActiveExplorer.Selection(1)
End If

'ReceivedTimeから年度の作成
Dim YearStr As String
If Month(objItem.ReceivedTime) > 3 Then
       YearStr = Format(objItem.ReceivedTime, "gee") & "年度"
Else
       YearStr = "" & Format("4/1/" & Year(objItem.ReceivedTime) - 1 & "", "gee") & "年度" 
End If

ゴリ押しだけど、これで受信年度を取得できるはず。
2019年に限っては、平成31年度と令和1年度が混在してめんどくさいけど。