Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Aug 2002
    Location
    Sierra Madre, California, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find/Replace CreateDate Field (Word 2K/XP)

    I am trying to write some VBA so that a user can find and replace all of the CreateDate fields in a document. Some of the fields are in the body of the document, others are in the Header/Footer. I use the CreateDate field so the date in the document won't automatically update. Unfortunately, I now have a request to be able to change the date to the current date (sometimes, they don't send the letter the day they created it). What I would like to do is:

    1. Find each CreateDate field
    2. Replace it with the current date not as a field

    What I have so far is:

    Sub ConvertCreateDate()
    Dim oField as Field
    For Each oField in ActiveDocument.Fields
    If oField.Type=wdFieldCreateDate Then
    Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:=False
    End If
    Next oField
    End Sub

    This works except that it doesn't search/replace the fields in the Headers/Footers and I'm not sure how to add that into the code. Any suggestions?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find/Replace CreateDate Field (Word 2K/XP)

    Try this:

    Sub ConvertCreateDate()
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter

    ChangeCreateDate ActiveDocument.Fields

    For Each oSection In ActiveDocument.Sections
    For Each oHeader In oSection.Headers
    If oHeader.Exists Then
    ChangeCreateDate oHeader.Range.Fields
    End If
    Next oHeader

    For Each oFooter In oSection.Footers
    If oFooter.Exists Then
    ChangeCreateDate oFooter.Range.Fields
    End If
    Next oFooter
    Next oSection
    End Sub

    Private Sub ChangeCreateDate(oFields As Fields)
    Dim oField As Field
    For Each oField In oFields
    If oField.Type = wdFieldCreateDate Then
    oField.Select
    Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:=False
    End If
    Next oField
    End Sub

    This will leave your document in Normal view and in the last header/footer; if you wish, you can record code to return to the view you prefer and paste the recorded code into the ConvertCreateDate macro.

  3. #3
    Lounger
    Join Date
    Aug 2002
    Location
    Sierra Madre, California, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find/Replace CreateDate Field (Word 2K/XP)

    Hans,

    It works great! I took your suggestion and added some code to get it back to PrintLayout view.

    Thank you! <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

Posting Permissions

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