【发布时间】:2016-07-07 11:34:34
【问题描述】:
我正在尝试使用 VBA 在 Microsoft Word 2013 中创建一个宏,以便在日期(主要是日期和月份)中添加前导零
所以从 2004 年 4 月 6 日到 2004 年 4 月 6 日 以及 2005 年 5 月 11 日至 2005 年 5 月 11 日
我在这里尝试使用答案的变体,但它在第一次约会时无休止地循环:http://answers.microsoft.com/en-us/office/forum/office_2010-word/macro-to-convert-date-format-in-a-document/20539af7-a961-499f-9e85-22af8f4c3c58?auth=1
Sub ConvertDateFormat()
Dim FoundOne As Boolean
Selection.HomeKey Unit:=wdStory, Extend:=wdMove
FoundOne = True ' loop at least once
Do While FoundOne ' loop until no date is found
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
.Format = True
.Forward = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceNone
' check that the find is a date
If IsDate(Selection.Text) Then
Selection.Text = Format(Selection.Text, "dd/mm/yyyy")
Selection.Collapse wdCollapseEnd
Else ' not a date - end loop
FoundOne = False
End If
Loop
End Sub
【问题讨论】:
标签: vba date ms-word leading-zero