【问题标题】:Macro to add leading zeroes to date with Tracking on - Microsoft VBA使用 Tracking on 添加前导零的宏 - Microsoft VBA
【发布时间】:2016-03-22 22:23:30
【问题描述】:

以下代码在不启用 Track Changes 的情况下工作。必须进行哪些更改才能在宏运行时触发“跟踪更改”,并且脚本不会在第一个结果上无休止地循环?

Sub ConvertDateFormat()
With ActiveDocument.Range
With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
    .Format = True
    .Wrap = wdFindStop
    .Forward = True
    .MatchWildcards = True
    .Execute
    End With
    Do While .Find.Found

If IsDate(.Text) Then
    .Text = Format(.Text, "dd/mm/yyyy")
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub

以上解决方案来自: Working with Microsoft Word VBA - macro to add leading zeroes to date

【问题讨论】:

  • 您需要提供有关此宏应在什么上运行、当前为您提供的结果以及所需结果的详细信息。确切地说,“跟踪更改以触发宏运行”是什么意思?

标签: vba ms-word


【解决方案1】:

您需要提供整洁的详细信息。如果我对您的问题的理解正确,

“脚本不会在第一个结果上无限循环”,

我认为,当启用跟踪更改时,查找和替换单词会替换整个日期,因此当您在 .find .execute 折叠时,新的替换日期会再次出现,因此它会无限循环。您需要再添加一个 .find.execute 以跳过插入的新日期。我认为这将解决问题。

Sub TestTrackOn()
With ActiveDocument.Range
With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
    .Format = True
    .Wrap = wdFindStop
    .Forward = True
    .MatchWildcards = True
    .Execute
    End With
    Do While .Find.Found
If IsDate(.Text) Then
    .Text = Format(.Text, "dd/mm/yyyy")
End If
.Collapse wdCollapseEnd
.Find.Execute
.Find.Execute
Loop
End With
End Sub

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 2021-11-21
    • 2015-05-18
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2021-11-26
    相关资源
    最近更新 更多