The question often comes up on how to automatically add the date to the name of a new document. This article addresses vba/macro methods to be inserted into document or global templates. Here is a link to the question that prompted writing the article. I plan on supplementing this at some point, so please check back. The link to this article is included in the principal macro to make checking for updates easier. If you find this article helpful, please do note that.
Scope: All desktop versions of Word that use vba
Level: Intermediate - Advanced
I have a macro set that I use to generate the date at the end of whatever is in the Title property.
The principal macro is stored in a Global Template.
The FileSave and FileSaveAs procedures can be in the Global Template if it is to apply to all new documents or can be only in individual .dotm macro-enabled templates, in which case it runs only on those templates.
I have included these in a Global Template that you can download from this temporary link.
Here are the Interception macros:
See Intercepting events like Save and Print for the ideas behind these.
Again, if you want to use this for all documents (as I do) these go into the Global Template, if only for documents that are based on particular document templates, these Interception macros go in those document templates and not in the Global Template. These specify no default names.
Sub FileSave() Application.Run "SaveWithDate"End Sub
Sub FileSaveAs() Application.Run "SaveWithDate"End SubHere is the principal macro that appends the date to the proposed filename:
This macro will NOT show up on a macro listing in the user interface.
This is designed to be called from other macros and can have a proposed filename included when so called.
If no proposed filename is included in the call then the wording in the document's Title Document Property will be used as the base to which the date is appended.
This macro proposes but does not impose the name. That is, the user is free to change it before saving.
I often use macros in the document templates to alter the Title Document Property before this is run. They pick content from Content Controls in the document containing names or other information to be added. I will try to add one or two of those later below.
Public Sub SaveWithDate(Optional strName As String) ' Charles Kenyon 2002-2023 ' https://answers.microsoft.com/en-us/thread/updatethread?forum=msoffice&threadId=ac0d9cce-f3fa-48ad-a702-e7816b5dfe94&editType=EditThreadByOwner&messageId=00000000-0000-0000-0000-000000000000 ' Assistance in writing this over the years from many on the Microsft Word online community likely including: ' Graham Mayor ' Doug Robbins ' Greg Maxey ' Dave Rado ' Jay Freedman ' Beth Melton ' Ibby ' Bill Coan ' (I'm sure I've omitted someone) ' ' Run as substitute for FileSave to add date to default document names ' Put in a document or global template as public macro ' ' Call this from other macros ' If strName is supplied, it is used as the base name, otherwise the base comes from the Title Property ' ' To use as Intercept, call from procedures named FileSave and FileSaveAs - see below ' Dim dlgSave As Dialog ' On Error Resume Next ' If Len(ActiveDocument.Path) > 0 Then ' The document has already been saved at least once. ' Just save and exit the macro. ActiveDocument.Save Exit Sub End If ' If strName = "" Then strName = strName & ActiveDocument.BuiltInDocumentProperties("Title").Value 'get name in Title Doc Prop ActiveDocument.BuiltInDocumentProperties("Title").Value = strName ' ' ADD THE DATE IN THE FORMAT "YYYY-MM-DD" ' This format can be modified to suit. I find that the one above sorts nicely, though, and is readable. strName = strName & " " & Format((Year(Now() + 1) Mod 100), "20##") & "-" & _ Format((Month(Now() + 1) Mod 100), "0#") & "-" & _ Format((Day(Now()) Mod 100), "0#") ' Set dlgSave = Dialogs(wdDialogFileSaveAs) With dlgSave .Name = strName .Show End WithEnd Sub
See Instructions for Installing Macros from Forums or Websites by Graham Mayor, MVP.
This macro set is in a Global Template stored in the Word Startup Folder and runs whenever the Save command is used or when the user tries to close the document and is queried about saving changes and then answers "Yes." It does not change the name for documents already saved.
Note that my macro set applies to all new documents because it is stored in a global template.
If you want it only for your templates you would need to save them as .dotm templates and use the FileSave and FileSaveAs procedures in your document templates and then delete those procedures from the global template. Then it would only be used in documents created from those specific templates.
Still use the global template to hold the main (SaveWithDate) macro. That way if you make any changes, you do not need to change all of your document templates.
Acknowledgements:
These have been written over a period of more than twenty years with the assistance of many in the Word online community and reliance on the MVP website.