我正在尝试使用 VBA 在 Microsoft Word 2013 中创建一个宏,以在日期(主要是日期和月份)中添加前导零
所以从 4/6/2004 到 04/06/2004 和 5/11/2005 到 05/11/2005
我试图在这里使用答案的变体,但它在第一个日期无休止地循环: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