Consulting

Results 1 to 4 of 4

Thread: Solved: Replace text with date

  1. #1
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location

    Solved: Replace text with date

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Try:

    [VBA]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
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Greg,

    Works perfectly.
    I appreciate your help.

    Regards,
    Dave T

  4. #4
    VBAX Regular
    Joined
    Nov 2020
    Posts
    15
    Location
    2021 - works perfectly on a mac! Yay!

    Thanks, Ashley

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •