Office Space: Tips and Tricks for Scripting Microsoft Office Applications
Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to email@example.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.
Inserting a Date-Time Field into a Word Document
The whole idea of using a script is to make things easier; if a script makes things harder to do, well, why even bother? It’s that philosophy (one we Scripting Guys recommend, by the way) that might lead you to question the subject matter for today’s column. After all, why learn some new Word-specific method for inserting the date and time into a document? Doesn’t VBScript already have the Date, Time, and Now functions? Why not just use those? Aren’t you making things harder for us rather than easier?
Well, not necessarily, and for two reasons. First, we sometimes get weird results using the VBScript functions. For example, this seemingly-innocuous little script generates a “Type mismatch” error:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText Now
To tell you the truth, we’re not sure why this happens, although we can work around the problem by doing this:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText "" & Now
If you’re trying to figure out what we did different, we changed the last line of code: instead of trying to type in the value of Now we instead type in an empty string (“”) and then tack on the value of Now. For some reason Word doesn’t like us passing just variables to the TypeText method, but it will happily accept an empty string and a variable.
But even though we have a workaround there’s another reason for using a Word-specific method to insert dates and times. Our preceding script inserts the date and time as a hard-coded value. Often times that’s exactly what you want. However, there might also be times when you want to insert a date-time field; that is, a value that updates itself to display the current date and time each time the document is opened (or each time you choose to update fields). The Word InsertDateTime method gives you the option of inserting either a hard-coded date value or a date-time field.
For example, here’s a script that inserts a date-time field, with a specific formatting we’ll discuss momentarily:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.InsertDateTime "M/d/yyyy h:mm:ss am/pm"
Run this script and you’ll get a Word document that looks like this:
And you didn’t think today’s column would be exciting.
The script itself is very simple. We create an instance of the Word.Application object, set the Visible property to True, then add a new, blank document. We create an instance of the Word Selection object, and then insert the date-time field using this line of code:
objSelection.InsertDateTime("M/d/yyyy h:mm:ss am/pm")
What if we didn’t want to insert a date-time field, what if we wanted to hard-code the date-time value in the document? In that case, we’d add a second, optional parameter (FALSE) which simply says, “Please don’t enter this as a date-time field.” That script would look like this:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.InsertDateTime "M/d/yyyy h:mm:ss am/pm", FALSE
Cool, huh? But what about that first parameter, what about that "M/d/yyyy h:mm:ss am/pm" business?
As you might have guessed, this first parameter specifies the format to be used when displaying the date-time information. This parameter is also optional; if you leave it out and just call InsertDateTime by itself the date-time value will be inserted using the short date-time format specified in your computer’s regional settings. (To view these settings, open the Regional and Language Options item in Control Panel.) For this script we wanted to display the date and time in a format similar to this, so we had to specify a formatting string:
4/20/2005 12:13:21 PM
But how did we know that the formatting string M/d/yyyy h:mm:ss am/pm would give us this kind of output? That’s easy: inside Word we clicked Insert and then clicked Field. In the Field dialog box we clicked Date, and then took a look at all the available date formats. As you can see, any time you click a format the formatting string is displayed near the top of the dialog box:
We just copied the formatting string directly from the dialog box and inserted it into our script.
Incidentally, you’re not limited to these formats; you can let your imagination run wild and create your own date-time formats. For example, take a look at this formatting string:
We’re not totally sure why you’d ever want to use it, but this formatting string will give you output that looks like this:
Let’s see VBScript’s Now function give you output that looks like that!