etech0
asked on
Word vba search for current date
I'm looking for VBA code that will search the document, checking if today's date appears anywhere.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Sorry, I'm so busy that I havn't answer your question.
if you want search for the date in this format: November 3, 2011, like this:
if you want to it deal with dnamic date, just replace
.Text = "2011-10-03" to .Text = Format(Date, "YYYY-MM-DD")
call 'Date' will return the date of today, and 'Format ' will transform it to string.
for example , today is 2011-11-08, the word text is :
Aaaaaa 2011-11-08 bbbbbbbbbb 2011-11-08 cccccc 2011-11-7 2011-10-03
the vba code is:
the result is:
if tomorrow you run the code , there will be 0 items found. Hope it helps.
best regards.
if you want search for the date in this format: November 3, 2011, like this:
Sub FindWords()
ActiveDocument.Paragraphs(1).Range.Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = "2011-10-03"
.Forward = True
End With
Do While True
Selection.Find.Execute
If Selection.Find.Found Then
intFound = intFound + 1
Else
Exit Do
End If
Loop
MsgBox intFound & " item(s) found!"
End Sub
if you want to it deal with dnamic date, just replace
.Text = "2011-10-03" to .Text = Format(Date, "YYYY-MM-DD")
call 'Date' will return the date of today, and 'Format ' will transform it to string.
for example , today is 2011-11-08, the word text is :
Aaaaaa 2011-11-08 bbbbbbbbbb 2011-11-08 cccccc 2011-11-7 2011-10-03
the vba code is:
Sub FindWords()
ActiveDocument.Paragraphs(1).Range.Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = Format(Date, "YYYY-MM-DD")
.Forward = True
End With
Do While True
Selection.Find.Execute
If Selection.Find.Found Then
intFound = intFound + 1
Else
Exit Do
End If
Loop
MsgBox intFound & " item(s) found!"
End Sub
the result is:
if tomorrow you run the code , there will be 0 items found. Hope it helps.
best regards.
ASKER
Can you edit it to search for the date in this format: November 3, 2011?
Thanks!