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
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 = 1Set 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 = TrueSet 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.
0 comments