PDA

View Full Version : Solved: Replace text with date



Dave T
08-05-2011, 06:46 AM
Hello All,

Currently I use the following macro to insert today's date into a Word 2003 document


Sub LongDateFixed()
'
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DATE \@ ""dd-MMM-yyyy""", PreserveFormatting:=True
Selection.TypeBackspace
Selection.Fields.Unlink
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub



In a document I frequently use there are two date placeholders that I use the macro on separately to replace/insert today's date.
…../…../…..

I have looked at Word's 'Find and Replace' option, where I can find the dotted lines but there is no option of replacing them with today's date.

Can someone please help with a macro that will replace both instaces of …../…../….. with todays date in the format of 05-Aug-2011 i.e. dd-MM-yyyy

Thanking you in advance,
Dave T

gmaxey
08-05-2011, 09:06 AM
Try:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "...../...../....."
While .Execute
oRng.Text = Format(Now, "dd-MMM-yyyy")
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Dave T
08-07-2011, 09:32 PM
Hello Greg,

Works perfectly.
I appreciate your help.

Regards,
Dave T

AshZM
10-01-2021, 03:43 PM
2021 - works perfectly on a mac! Yay!

Thanks, Ashley