How Can I Add the Last-Saved Date to the Footer of a Microsoft Word Document?

How Can I Add the Last-Saved Date to the Footer of a Microsoft Word Document?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I put the date that a Microsoft Word document was last saved in the footer of that document? Oh: and how can I center that footer while I’m at it?

-- GH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GH. Well, the best way to go about – say, wait a second here: that sounds suspiciously like two questions, one about putting the last-saved date in the footer, the other about centering that footer. You’re not trying to trick the Scripting Guys and get a two-for-the-price of one deal, are you?

If you are, well, that’s OK; don’t worry about it. After all, now that the 2007 Winter Scripting Games are complete the Scripting Guys have nothing else to do anyway.

Well, other than finish testing all the entries and then total up the final results. And draw names and mail out 250 Dr. Scripto bobblehead dolls and 20 copies of Windows PowerShell in Action. And put together a couple of follow-up articles on the Games and how people went about solving the various challenges. And ….

Editor’s Note: Anyone else getting the feeling that the Scripting Guy who writes this column is getting a little whiney these days?

You know, that does sound like a lot of work, doesn’t it? But any time there’s work to be done there’s one thing you can rely on the Scripting Guy who writes this column to do: spend his time answering your questions about Microsoft Word footers and letting Scripting Guy Jean Ross take care of all that Scripting Games stuff. Here’s an answer to your two questions, GH. That takes care of our end of the bargain. And we’re sure that Jean will work day and night in order to complete her responsibilities as well.

Here’s the script:

Const wdFieldSaveDate = 22
Const wdAlignParagraphCenter = 1

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Sections(1).Footers(1).Range
objDoc.Fields.Add objRange, wdFieldSaveDate

objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter

objDoc.SaveAs("C:\Scripts\Test.doc")

Thanks, and see you tomorrow.

What’s that? Explain how this script works? Oh, come on, GH. After all, we already wrote the script; shouldn’t Jean have to do something here, too?

OK, fine. As you can see, we start out by defining a pair of constants: wdFieldSaveDate (with a value of 22), which we’ll use to insert the date that the document was last saved; and wdAlignParagraphCenter (with a value of 1), which we’ll use to center the footer. We then use the following block of code to create an instance of the Word.Application object, make this instance visible onscreen, and then create a new document:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Add()

Now we’re ready to add the last-saved date to the footer of this new document. To begin with, we use this line of code to create an object reference to the first (and, in this case, only) footer in the document:

Set objRange = objDoc.Sections(1).Footers(1).Range

Once we have an object reference to the footer range we can call the Add method of the Fields collection and add the last-saved date:

objDoc.Fields.Add objRange, wdFieldSaveDate

In case you’re wondering (and you probably are), adding the last-saved date to a document using a script works very similar to the process of adding that value from within Word itself. If you’re working in Microsoft Word you can add the last-saved date to a document by (in Word 2003) selecting Field from the Insert menu and then choosing SaveDate. Here we’re doing essentially the same thing, calling the Add method and then passing along two parameters: the object reference to the location where we want to add the field; and the constant representing the field we want to add. How did we know that we need to use the constant wdFieldSaveDate, with a value of 22, in order to add the last-saved date to the footer? To tell you the truth, we didn’t; that’s why we looked this up in the Microsoft Word VBA Language Reference (look for wdFieldType).

So much for question 1; that’s all we had to do to add the last-saved date to the footer. Now let’s tackle question 2, something we can take care of with a single line of code:

objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter

As you can see, we’re again working with the footer range (note the object reference objRange). This time, though, we’re accessing the ParagraphFormat object and assigning a value to the Alignment property. How did we know that the constant wdAlignParagraphCenter, with a value of 1, would give us centered alignment. That’s right: we didn’t. (Hint: You’ll rarely go wrong by assuming that the Scripting Guys don’t know much of anything.) Once again, however, it’s the Microsoft Word VBA Language Reference to the rescue (look for wdParagraphAlignment).

In the very last line of the script we then use the SaveAs method to save the document as C:\Scripts\Test.doc. We do that simply so you can verify that the script worked; after all, if the document has never been saved then it won’t have a last-saved date.

We hope that answers both your questions, GH. Which reminds us: we actually did do two things here, didn’t we? Looks like Jean’s not only going to have to finish up with the 2007 Scripting Games, but she’ll have to take care of the 2008 Scripting Games as well. But hey, fair is fair, right?

Editor’s Note: Maybe with Jean taking over everything the 2008 Scripting Games won’t have another Event 8 episode like we saw this year. We won’t mention who came up with that one.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • How about adding the Last Saved or Updated date to the footer of an Excel worksheet?

    Thanks,

    B06SAJ

  • B06SAJ: While editing your footer, just go to Insert, Fields, and select SaveDate, click OK and Word will insert that field (or any other you select) into cursor location.