Stop automatic date update

Home Up Search This Site What's New? Audio On CDR Favourites Downloadable files Photo Gallery 2002 Photo Gallery 2003 Photo Gallery 2004/5 Photo Gallery 2006/7 Photo Gallery 2008 Photo Gallery 2009/10 Photo Gallery 2011 Photo Gallery 2012 UK Photo Gallery Ireland Photo Gallery Cats Photo Gallery 

horizontal rule

 

 

Google
 

 

Many people access the material from this web site daily. Most just take what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved, however small, would help to ensure the continued availability of this resource.

Click the appropriate button above to access PayPal.

 

 

Stop automatic date update!

 

Users frequently complain that dates they have inserted into Word documents update automatically when the document is opened. The reason is that they have inserted a DATE field which shows the system date. What is needed is change those date fields for CREATEDATE fields, which when updated will display the dates the documents were created -

ALT+F9 change { DATE } to { CREATEDATE \@ "d MMMM yyyy" } then F9 and ALT+F9 - and change the date in the template so that future documents based on it show the correct dates. The first of the macro listings below will change any DATE field in an open document or template to a CREATEDATE field. The switches \@ "d MMMM yyyy" may be different at your location.

However you may now have a folder full of documents containing DATE fields and may wish to process them all in a batch. The second macro uses the code from the first macro and applies it to all the files in a chosen folder.

Create a new folder and copy the files that you wish to change into it, before running the macro. It is always advisable to work with copies when batch processing, as you could destroy a lot of work if there is an unexpected problem!

Note:

It is possible to use a TIME field to display the system date e.g. { TIME \@ "dd/MM/yyyy" } will display the date in the format of the switch. If the document uses TIME rather than DATE fields, it will be necessary to change the  field references in the macros accordingly.

 

Sub FixDates()
Dim sSwitch As String
Dim
iFld As Long

'Set the required date switch - note the quotes
sSwitch = " \@ ""d MMMM yyyy"""

'Check each field in the document
For
iFld = ActiveDocument.Fields.Count To 1 Step -1
     With
ActiveDocument.Fields(iFld)
          Select Case
.Type

          'The field is a Date field
          Case Is =
wdFieldDate

               'Change it to a Createdate field
              
.Code.Text = Replace(UCase(.Code.Text), _
                   "DATE", "CREATEDATE")

               'If there is no date switch - add one
               If
InStr(1, .Code, "\@") = 0 Then
                   
.Code.Text = .Code.Text & sSwitch
               End If
              
.Update

          Case Else
          End Select
     End With
Next
iFld
End Sub
 

Sub BatchFixDates()
Dim
strFile As String
Dim
strPath As String
Dim
sSwitch As String
Dim
strDoc As Document
Dim
iFld As Long
Dim
fDialog As FileDialog

'Set the required date switch - note the quotes
sSwitch = " \@ ""d MMMM yyyy"""
Set
fDialog = Application.FileDialog(msoFileDialogFolderPicker)

'Get the folder containing the documents
With
fDialog
    
.title = "Select folder containing the documents to be processed and click OK"
     .AllowMultiSelect = False
     .InitialView = msoFileDialogViewList

     If
.Show <> -1 Then
         
MsgBox "Cancelled By User", , "List Folder Contents"
          Exit Sub
     End If
    
strPath = fDialog.SelectedItems.Item(1)
     If
Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

'If there are any open documents, close them (prompting to save changes)
If
Documents.Count > 0 Then
    
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
strFile = Dir$(strPath & "*.doc")

'Open each document in the folder
While
strFile <> ""
     Set
strDoc = Documents.Open(strPath & strFile)

     'Check each field in the document
     For
iFld = ActiveDocument.Fields.Count To 1 Step -1
          With
ActiveDocument.Fields(iFld)
              
'The field is a Date field

               Select Case .Type
               Case Is
= wdFieldDate

                    'Change it to a Createdate field
                   
.Code.Text = Replace(UCase(.Code.Text), _
                        "DATE", "CREATEDATE")

                    'If there is no date switch - add one
                    If
InStr(1, .Code, "\@") = 0 Then
                        
.Code.Text = .Code.Text & sSwitch
                    End If

                    .Update
               Case Else
               End Select
          End With
     Next
iFld

     'Save the changes
    
strDoc.Close SaveChanges:=wdSaveChanges
     strFile = Dir$()

Wend
End Sub

Displaying the date a document was last modified.

 

Frankly if you are re-using a document on a regular basis and want to retain the date that the document was saved, you would be better off saving the document as a template and create new documents from it, each of which would have its own creation date. You would also have a complete record of the update process - see also Save Numbered Versions

Word includes a SAVEDATE field which displays the last saved date (and optionally the time) which you could use in place of CREATEDATE in the above macros. However, if having inserted the field that is the date that you want to retain next time you open the document, and you then save the document to include the field, the SAVEDATE will now display its revised date content which is no longer the date you wanted.

Note:

The macros all use UK pattern short dates, you can change the switches and formats, as indicated, to include the save time if you wish. See Formatting Word fields with switches

 

It would be preferable to be able to display the last saved date in a document today and have that date available and secure next time the document is opened. You can do this by assigning the previous save date, stored in the document's properties to a document variable BEFORE you save the changes and you can display the information from the document variable with a DOCVARIABLE field

This will require a macro to setup the document initially and the Save and SaveAs routines in the document template would need to be intercepted with code to update the variables with the previous save dates when next the document is modified and saved.

The following example displays the date in the document with the docvariable field { DocVariable varModifiedDate }.

To change the date fields in the document to docvariable fields and set the variable to the last saved date you would need to run one of the following macros FIRST (before installing the  FileSave and FileSaveAs macros listed at the end of this page).

This first macro is intended to replace Date fields in the document. It reads the last saved date from the document's properties and applies that date as the initial value for the document variable, then saves and closes the document. When the document is next opened, the last saved date (from before this field change) will be displayed. The new file save/saveas macros will then update the field to the current date whenever they are used.

 

Sub FixDates2()

'This macro is used to replace DATE fields in the document
Dim iFld As Long
'Set the initial value of the variable
ActiveDocument.Variables("varModifiedDate").Value = _
Format(ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved), "dd/MM/yyyy") 'or

'Format(ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved), "dd/MM/yyyy hh:mm")
'Check each field in the document
For iFld = ActiveDocument.Fields.Count To 1 Step -1
    With ActiveDocument.Fields(iFld)
        Select Case .Type
        'The field is a Date field
            Case Is = wdFieldDate
                'Change it to a DocVariable field
                .Code.Text = " DOCVARIABLE varModifiedDate "
                'or if you have different formatting switches on your date fields
                '.Code.Text = Replace(.Code.Text, "Date", "DATE")
                '.Code.Text = Replace(.Code.Text, _
                "DATE", "DOCVARIABLE varModifiedDate")

                .Update
            Case Else
        End Select
    End With
Next
iFld
ActiveDocument.Close wdSaveChanges
End Sub

Note:

The iFld loop in the macro above can be used in place of the iFld loop in the earlier batch macro to replace the Date fields with docvariables in a range of documents

 

Sub AddDates()
'This macro is used to insert a DocVariable field in the document
If Len(ActiveDocument.Path) = 0 Then
    'The document has not previously been saved
    'So save it

    ActiveDocument.Save
End If
'Set the initial value of the variable
ActiveDocument.Variables("varModifiedDate").Value = _
    Format(ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved), "dd/MM/yyyy") 'or

    'Format(ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved), "dd/MM/yyyy hh:mm")
'Insert a field at the cursor
Selection.Range.Fields.Add Selection.Range, _
    Type:=wdFieldDocVariable, _
    Text:="varModifiedDate", _
    PreserveFormatting:=False
'Save the field in the document
ActiveDocument.Close wdSaveChanges
End Sub

 

The following macros are stored in the document template and replace the in-built save routines to ensure that the document variable is updated whenever the document is saved.

 

Sub FileSave()
Dim oVars As Variables
Dim
oField As Field
Set oVars = ActiveDocument.Variables
oVars("varModifiedDate").Value = Format(Date, "dd/MM/yyyy") '& _
Format(Time, " hh:mm")

For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldDocVariable Then oField.Update
Next oField
On Error Resume Next
ActiveDocument.Save
ActiveWindow.Caption = ActiveDocument.FullName
End Sub

 

Sub FileSaveAs()
Dim oVars As Variables
Dim oField As Field
Set oVars = ActiveDocument.Variables
oVars("varModifiedDate").Value = Format(Date, "dd/MM/yyyy") '& _
Format(Time, " hh:mm")

For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldDocVariable Then oField.Update
Next oField
On Error Resume Next
Dialogs(wdDialogFileSaveAs).Show
ActiveWindow.Caption = ActiveDocument.FullName
End Sub

Note: The macros above on this page assume that the fields are in the main document area. If they are located elsewhere the macros may not access them without additional code to process the story ranges containing them. For more information about installing macros use this link.  (The example code used in that link would suffice to update fields not in the document body).

Locking the Date fields

 

An alternative approach could use the option to lock the date field. A locked field will not automatically (or manually) update. To adopt this approach would entail intercepting the FileSave and FileSaveAs routines as shown in the previous example, but in this instance instead of updating a document variable and associated field, the date fields are retained and are unlocked, updated then relocked before saving the document. This ensure that the document will always display the last saved date (and time if a time switch is included on the Date field). The macros include the option to prompt to update the fields, should that be preferred.

 

Sub FileSave()
Dim oStory As Range
Dim
oField As Field

'Dim sUpdate As String
'sUpdate = MsgBox("Update the date field(s)?", vbYesNo, "Save Document")
'If sUpdate = vbYes Then

For Each oStory In ActiveDocument.StoryRanges
    For Each oField In oStory.Fields
        If oField.Type = wdFieldDate Then
            oField.Locked = False
            oField.Update
            oField.Locked = True
        End If
    Next
oField
    oStory.Fields.Locked = True
    If oStory.StoryType <> wdMainTextStory Then
        While Not
(oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            For Each oField In oStory.Fields
                If oField.Type = wdFieldDate Then
                    oField.Locked = False
                    oField.Update
                    oField.Locked = True
                End If
            Next
oField
        Wend
    End If
Next
oStory

'End If

On Error Resume Next

ActiveDocument.Save
ActiveWindow.Caption = ActiveDocument.FullName
Set oStory = Nothing

End Sub

 

Sub FileSaveAs()
Dim oStory As Range
Dim
oField As Field
'Dim sUpdate As String
'sUpdate = MsgBox("Update the date field(s)?", vbYesNo, "Save Document")
'If sUpdate = vbYes Then

For Each oStory In ActiveDocument.StoryRanges
    For Each oField In oStory.Fields
        If oField.Type = wdFieldDate Then
            oField.Locked = False
            oField.Update
            oField.Locked = True
        End If
    Next
oField
    oStory.Fields.Locked = True
    If oStory.StoryType <> wdMainTextStory Then
        While Not
(oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            For Each oField In oStory.Fields
                If oField.Type = wdFieldDate Then
                    oField.Locked = False
                    oField.Update
                    oField.Locked = True
                End If
            Next
oField
        Wend
    End If
Next
oStory
'End If

On Error Resume Next
Dialogs(wdDialogFileSaveAs).Show
ActiveWindow.Caption = ActiveDocument.FullName

Set oStory = Nothing

End Sub