Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing dates in template (WinWord)

Status
Not open for further replies.

Spiggsy

Technical User
Apr 12, 2001
59
0
0
CA
The problem we have is this:

Office Administrator has saved documents created from templates that auto-populate the date field within MS Word to todays date.
Now we need have documents that change the date to todays, rather than showing the original document's date.
We need to somehow convert these documents from utilising the todays date auto-population to retaining the original date entered.

Is there a way to do this ?

Thanks,

Spiggsy!
 
Part A:
We need to somehow convert these documents from utilising the todays date auto-population to
The difference between putting a date in Word as updating, and putting in as NOT updating, is the first inserts a field. The second insert simple text. If what you want is code to convert updating fields to text, this will do so. Although it will convert to the current date.
Code:
Sub ResetDateFields()
Dim oField As Word.Field
For Each oField In ActiveDocument.Fields()
 If oField.Type = 31 Then
    oField.Select
    Selection.Collapse direction:=wdCollapseStart
    oField.Delete
    Selection.InsertDateTime _
         DateTimeFormat:="dddd, MMMM dd, yyyy", _
         InsertAsField:=False
 End If
Next
End Sub

Part B:
retaining the original date entered.
There is absolutely no way of getting the original date entered. How could it? The field has been updated. It does not keep a record of past states. It is possible to record past states by code, but you don't have that code in place, and Word does not do it for you.

Now, if you were absolutely certain of what the "original" date was, then you could insert that as text, rather than the current date.

Gerry
 
Okay, that's what I thought I'd hear.
 
What you need to do is to change the Date field in the template to the CreateDate field. The CreateDate means just what it says: when you create a new document from the template, the fields will be populated with the current date and will never change (because that is the createdate the document has been created which cannot be any other day than the day it was created).

Providing the documents you already have were created from a template, then you should be able to substitute the dates with the CreteDate field too and they will reflect the original create date correctly and not the updated current date.

To test this, just enter the CreateDate anywhere in one of the documents to see the result.


Regards: tf1
 
I thought the question was regarding making adjustments to EXISTING documents, not new ones. Of course if you want to make changes to any new documents created, you need to change the template.

Gerry
 
Hi Gerry

I agree the question infers 'current documents' but I added my post to make sure that the practise doesn't continue!

Terry

Regards: tf1
 
Whether you're dealing with templates or existing documents, the solution depends on whether the date are plain text or fields.

If the dates are plain text, you'd need some consistency about where the dates might be in order to be able to code a solution to replace the right ones. For example, they may appear in the header or footer, or only in the the first or last 'n' lines of the document, or they could be attached to a bookmark used by an Autotext entry.

If the dates are fields, it's much easier. Relatively static dates are likely to use CREATEDATE or PRINTDATE fields, depending on whether the document is intended to show the date created or the date last printed. More volatile dates might use SAVEDATE or DATE fields. Changing these is as easy as changing the field code. For example:

Code:
Sub ChangeDateFields()
Dim oRange As Word.Range    ' All Range objects - includes ranges in the body, headers, footers & shapes
Dim oField As Word.Field    ' All Field objects
Dim MyRange
With ActiveDocument
    ' Loop through all range objects
    For Each oRange In .StoryRanges
        ' Loop through all range objects
        For Each oField In oRange.Fields
            ' Test for & change DATE fields to CREATEDATE fields
            ' Type 31= wdFieldDate, Type 21= wdFieldCreateDate
            If oField.Type = 31 Then
                MyRange = oField
                oField.Delete                 '
                .Fields.Add Range:=MyRange, Type:=21, PreserveFormatting:=False
            End If
        Next oField
    Next oRange
End With

Cheers
 
That will still replace a field with the current date though, correct? Not any "original" date.

Gerry
 
Hi Gerry,

Correct only if the current date is the original date.

If you're working on a document with a DATE field, the macro will change it to a CREATEDATE field. In that case, you'll only have the current date on the day the document is created. Open the document the next day and you'll see the date created (ie the 'original' date), not the current date.

Depending on what the OP wants, the code could be changed to replace any form of the DATE field with any other form - the code was just an example of how one might go about doing that.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top