OKの場合はスルーするようにしました。
むろんスルーすると、チェック対象から外れたのか、
OKなのかが区別できません。
もし、スルーしたくなったら、
'MsgBox "OK:" & _
' Format(GetDay(Selection.Range.Text), "GGGE年M月D日") & _
' Right(Selection.Range.Text, 3)
を
MsgBox "OK:" & _
Format(GetDay(Selection.Range.Text), "GGGE年M月D日") & _
Right(Selection.Range.Text, 3)
と書き換えてください。
動作パターンを3つ用意しました。
一長一短がありますので、フィットするものを使ってください。
(選択しないパターンをコメントアウトしてください。)
パターン1
令和x年を考慮しないコードです。
パターン2
令和x年を考慮したコードですが、
ワードを開いて実行したときに、若干醜いです。
パターン3
令和x年を考慮したコードで、
ワードを開いて実行したときに見やすいものの、
9月3日(金)といった文字列がワード文章の先頭に来ると
それは検査の対象から漏れます。
以下のコードは、このパターン3のコードです。
Option Explicit
Const MyNen = 3 '年が省略されている場合に見なす令和の年
Sub DateCheck()
ActiveDocument.Bookmarks("\StartOfDoc").Select
With Selection.Find
'パターン1
'.Text = "([0-9| ]{1,2}月)([0-9| ]{1,2}日)([\(|()]?[(\)|)])"
'パターン2
'.Text = "*([0-9| ]{1,2}月)([0-9| ]{1,2}日)([\(|()]?[(\)|)])"
'パターン3
.Text = "?????([0-9| ]{1,2}月)([0-9| ]{1,2}日)([\(|()]?[(\)|)])"
.MatchFuzzy = False
.MatchWildcards = True
Do While .Execute
If Format(GetDay(Selection.Range.Text), "aaa") = _
Left(Right(Selection.Range.Text, 2), 1) Then
'OKの場合
'MsgBox "OK:" & _
' Format(GetDay(Selection.Range.Text), "GGGE年M月D日") & _
' Right(Selection.Range.Text, 3)
'NGの場合
Else
MsgBox "★NG:" & _
Format(GetDay(Selection.Range.Text), "GGGE年M月D日") & _
Right(Selection.Range.Text, 3)
End If
Loop
End With
ActiveDocument.Bookmarks("\StartOfDoc").Select
End Sub
'//------------------------------------------------
Function GetDay(InText As String) As Date '年月日を取得
Dim y As Long
Dim m As Long
Dim d As Long
y = GetY(InText)
m = GetM(InText)
d = GetD(InText)
GetDay = DateSerial(y + 2018, m, d)
End Function
'//------------------------------------------------
Function GetD(InText As String) As Long '日を取得
Dim i As Long
If IsNumeric(Left(Right(InText, 6), 1)) = False Then
GetD = Val(Left(Right(InText, 5), 1))
Else
GetD = Val(Left(Right(InText, 6), 2))
End If
End Function
'//------------------------------------------------
Function GetM(InText As String) As Long '月を取得
If Left(Right(InText, 7), 1) = "月" Then
If IsNumeric(Left(Right(InText, 9), 1)) = False Then
GetM = Val(Left(Right(InText, 8), 1))
Else
GetM = Val(Left(Right(InText, 9), 2))
End If
End If
If Left(Right(InText, 6), 1) = "月" Then
If IsNumeric(Left(Right(InText, 8), 1)) = False Then
GetM = Val(Left(Right(InText, 7), 1))
Else
GetM = Val(Left(Right(InText, 8), 2))
End If
End If
End Function
'//------------------------------------------------
Function GetY(InText As String) As Long '令和暦で年を取得
Dim i As Long
Dim FromY As Long
Dim ToY As Long
Dim wkY As Date
For i = 11 To 15
If Left(Right(InText, i), 2) = "令和" Then
FromY = i - 2
Exit For
End If
Next i
For i = 8 To 10
If Left(Right(InText, i), 2) = "年" Then
ToY = i + 1
Exit For
End If
Next i
wkY = Val(Left(Right(InText, FromY), FromY - ToY))
If wkY = 0 Then
GetY = MyNen
Else
GetY = wkY
End If
End Function
お礼
何度も本当にありがとうございます! 実際の会議資料で使わせていただいています。 ~で記載の部分も、チェック対象となりました。 ありがとうございます!!