Hey, Scripting Guy! How Can I Insert a Date Field in Office Word?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I insert a field in a Word document that shows the date two weeks from today?

— HO

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HO. As you’re reading this the Scripting Guy who writes this column is no doubt sitting in the bowels of the King County Superior Court, waiting to hear whether he actually gets assigned to a jury or not. We realize that many of you have never experienced the excitement of serving on a jury. With that in mind, here’s a recap of our hero’s first day as a Superior Court juror.

The Scripting Guy who writes this column woke up at 5:30 AM, looked out the window, and saw that it was snowing like crazy outside. Hoping for a postponement, he called the jury duty hotline to see if there were any weather-related delays. When there weren’t any, he sighed, took a shower, got dressed, and left the house a little after 6:00 AM; that was the only way he could be sure and reach downtown Seattle by 8:00 AM.

He arrived at the courthouse around 7:45 and, as instructed, went straight to the Juror Assembly room. There he waited with his fellow jurors until someone from Superior Court came out and made an announcement. “Apparently none of you got the message,” he said, “but about 7:45 or so we announced that court would be delayed for 2 hours. So I guess you’ll just have to sit here and wait until 10:00.”

And so the Scripting Guy who writes this column sat there and waited until 10:00. After officially checking in (a process that took only a couple minutes), he then sat there and waited until 12:00, when everyone was dismissed for lunch. After eating a sandwich he went back to the Juror Assembly room and sat there and waited until 2:30, at which time he was assigned to the juror pool for a particular trial. He then sat there and waited for another hour before he and the other members of the pool were instructed to go to the court room on the 7th floor. There they were told, “Well, normally we would do juror interviews at this point, but because we’re running late we’ll have to do those tomorrow. So you’ll all have to come back tomorrow.”

And then he went home.

In other words, as near as the Scripting Guy who writes this column can tell, jury duty involves sitting around bored, going to lunch, then sitting around bored until it’s time to go home. Fortunately, though, he had no problem with that; after all, that’s a pretty good description of his typical day at work.

However, he did have a little trouble figuring out how to insert a field in a Word document, or at least how to insert a field that shows the date two weeks from today. In fact, as near as he could tell there’s no way to do that, not even from within Word itself. But did the Scripting Guy who writes this column give up? You bet he did.

But later on, after finishing both the crossword puzzle and the daily Sudoko, and after reading Dear Abby for the third time, he came up with the following workaround:

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

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Set objRange = objDoc.Bookmarks("TwoWeeks").Range
objRange.Text = Date + 14

Because he couldn’t figure out a way to create a field that showed the date two weeks from today he did the next best thing: he inserted a bookmark (named TwoWeeks) into the document, then simply used a little programming wizardry to set the value of that bookmark to the date two weeks from today. A different approach, but the same net effect.

So how does the script work? Well, to begin with, we create an instance of the Word.Application object, then set the Visible property to True; that gives us a running instance of Microsoft Word that we can see onscreen. We then use the Open method to open the document C:\Scripts\Test.doc:

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Important. This script assumes that the document Test.doc already has a bookmark named TwoWeeks. If that bookmark doesn’t exist then the script will fail.

As soon as the document is open we use this line of code to create a Range object that encompasses the bookmark named TwoWeeks:

Set objRange = objDoc.Bookmarks("TwoWeeks").Range

At that point all we have to do is set the Text of the bookmark to the current date plus two weeks (that is, plus 14 days):

objRange.Text = Date + 14

And there you have it.

This actually works pretty well, although there is one drawback: the bookmark will be updated only if you open the document by using the preceding script. That’s because there’s no code within the document itself that will automatically update the bookmark. That may or may not be a problem. If it is a problem you can always create an AutoExec macro inside the document itself. As you probably know, an AutoExec macro runs each time a document is opened; that means that each time the document is opened the bookmark text will be updated.

Without explaining the ins and outs of using VBA (Visual Basic for Applications) inside Microsoft Word, here’s the code for our AutoExec macro:

Sub AutoExec()
    Set objRange = ActiveDocument.Bookmarks("TwoWeeks").Range
    objRange.Text = Date + 14
End Sub

Just the way Dear Abby would write it.

As for the Scripting Guy who writes this column, he’ll find out very soon whether he’s officially fulfilled his civic duty, or whether he gets to sit on a trial that’s expected to last – at a minimum –three weeks. That sure would be … fun …. But we’ll keep you posted; needless to say, we have plenty of time on our hands these days.


Discussion is closed.

Feedback usabilla icon