在填写日期的Word文档中搜索今天的日期

时间:2016-02-10 13:22:13

标签: vba date search ms-word

我的Word文档中填写了几年的日期。我想添加一个今天搜索的宏。日期格式为DD.MM.YYYY。我徒劳地试了很多东西。我尝试的最后一件事是:

Sub Today
    Dim c As String
    Set c = Format(Now(), "DD.MM.YYYY")
    c.Find.ClearFormatting
    c.Find.Execute
End Sub

但它不起作用......

2 个答案:

答案 0 :(得分:1)

这是你要找的吗?

Sub Demo123()
Application.ScreenUpdating = False
Dim s As String
s = Format(Now(), "DD.MM.YYYY")
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = s
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = False
    .Execute
  End With
  Do While .Find.Found
      .HighlightColorIndex = wdYellow
      'or do whatever you want
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

非常感谢,Rahul

你帮我看了路。我最终想出了这个,这就是诀窍。要回答你的问题,在找到之后就必须去那里。

Sub Today()
Dim s As String
s = Format(Now(), "DD.MM.YYYY^p")
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = s
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    Exit Sub
    Else
    MsgBox "Date not found."
    End If
End Sub

一切顺利,

威廉