|
- Sub 规范日期()
- Dim arr(3) As String, i As Byte
- Dim defSpace$, defNumber$, defMark$, sDate$
- defSpace = " " & Chr(9) & ChrW(160)
- defNumber = "0-90-9一二三四五六七八九十零〇○OoOo"
- defMark = ChrW(&H2013) & "—-" '注意:半角-需要放在最后
- arr(0) = "^13[" & defSpace & defNumber & "]@年[" & defSpace & defNumber & "]@月[^13^12]"
- arr(1) = "^13[" & defSpace & defNumber & "]@年[" & defSpace & defNumber & "]@月[" & defSpace & defNumber & "]@日[^13^12]"
- arr(2) = "^13[" & defSpace & defNumber & "]@[" & defMark & "][" & defSpace & defNumber & "]@[^13^12]"
- arr(3) = "^13[" & defSpace & defNumber & "]@[" & defMark & "][" & defSpace & defNumber & "]@[" & defMark & "][" & defSpace & defNumber & "]@[^13^12]"
- For i = 0 To UBound(arr)
- With ActiveDocument.Content.Find
- .ClearFormatting
- .Forward = True
- .MatchWildcards = True
- .Wrap = wdFindStop
- .Text = arr(i)
- Do While .Execute
- With .Parent
- .Start = .Start + 1: .End = .End - 1
- sDate = 删除空白(.Text, defSpace)
- If i > 1 Then sDate = 转换年月(sDate & IIf(i = 2, "月", "日"), defMark)
- sDate = 日期转换(sDate)
- If Len(sDate) = 0 Then .Font.ColorIndex = wdRed Else .Text = sDate
- .Start = .End
- End With
- Loop
- End With
- Next i
- End Sub
- Private Function 删除空白$(ByVal sDate$, ByVal ReplaceChar$)
- Dim i As Byte
- For i = 1 To Len(ReplaceChar)
- sDate = Replace(sDate, Mid(ReplaceChar, i, 1), "")
- Next i
- 删除空白 = sDate
- End Function
- Private Function 转换年月$(ByVal sDate$, ByVal mark$)
- Dim i, j, pos, pos1 As Byte, s As String * 1
- For i = 1 To 2
- s = IIf(i = 1, "年", "月"): pos = 0
- If InStr(sDate, s) = 0 Then
- For j = 1 To Len(mark)
- pos1 = InStr(sDate, Mid(mark, j, 1))
- If pos1 > 0 Then pos = IIf(pos > pos1, pos1, IIf(pos = 0, pos1, pos))
- Next j
- If pos > 0 Then sDate = Left(sDate, pos - 1) & s & Mid(sDate, pos + 1)
- End If
- Next i
- 转换年月 = sDate
- End Function
- Private Function 日期转换$(ByVal sDate$)
- Dim FindChar$, ReplaceChar$, i, pos As Byte
- FindChar = "0123456789零一二三四五六七八九〇○OoOo"
- ReplaceChar = "01234567890123456789000000"
- sDate = Replace(sDate, "三十日", "30日")
- sDate = Replace(sDate, "二十日", "20日")
- sDate = Replace(sDate, "十日", "10日")
- sDate = Replace(sDate, "十月", "10月")
- sDate = Replace(sDate, "年十", "年1")
- sDate = Replace(sDate, "月十", "月1")
- sDate = Replace(sDate, "十", "")
- For i = 1 To Len(sDate)
- pos = InStr(FindChar, Mid(sDate, i, 1))
- If pos > 0 Then sDate = Replace(sDate, Mid(FindChar, pos, 1), Mid(ReplaceChar, pos, 1))
- Next i
- sDate = Replace(sDate, "年0", "年")
- sDate = Replace(sDate, "月0", "月")
- 日期转换 = IIf(sDate Like "*[!0123456789年月日]*", "", sDate)
- End Function
复制代码
|
|