How Can I Put the File Name in the Footer of a Microsoft Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’ve seen Word documents where the file name appears in the footer. Except that this file name is dynamic: if the name of the document changes then the footer changes, too. How can I add that kind of a footer using a script?

— ZK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZK. You know, it’s always tough to come back to work after being gone for a week; there’s just so many things to catch up on. For example, we haven’t had the chance yet to mention the fact that, in a study of the 25 largest metropolitan areas in the U.S., Seattle drivers ranked 23rd out of 25 when it came to the rudest drivers. (In other words, we have very polite drivers here. The rudest drivers of all? Those from Miami.)

The Scripting Guy who writes this column finds this interesting, in part because none of his Scripting Siblings ever come over to the Seattle area to visit him. Why not? “No way am I driving with all those crazy Seattle drivers!” Except, as it turns out, Seattle drivers aren’t crazy drivers after all.

Hmmm; wonder why they really don’t ever come over to visit him?

Note. Actually there’s a simple, logical explanation why they don’t come over: driving from the Tri-Cities to Seattle is a long drive, too long to make for a weekend visit. Apparently, though, it’s way shorter and way easier for the Scripting Guy who writes this column to drive from Seattle to the Tri-Cities. And he’s expected to do so, even if that means driving over, attending some sort of family function, and then turning around and driving back.

So if Seattle-area drivers are nice does that mean that Seattle-area Scripting Guys are nice, too? And does that also mean that they’d be happy to answer a question about adding a dynamic file name to the footer of a Word document? Heck no. But they’ll answer the question anyway:

Const wdFieldFileName = 29

Set objWord = CreateObject(“Word.Application”) objWord.Visible = True

Set objDoc = objWord.Documents.Add()

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

objDoc.SaveAs “C:\Scripts\Test.doc”

OK. This script starts out by defining a constant named wdFieldFileName and setting the value to 29. As it turns out, Word includes a number of built-in “fields” that can display information such as file name, user name, author name, last-saved date, last-printed date, etc. The field that displays the file name has a value of 29; hence we assign our constant the value 29.

Note. Gosh, have the Scripting Guys memorized all these Word fields and their corresponding values? No, of course not, we – uh, yes, yes we have; that’s what makes us the Scripting Guys. For the rest of you, though, those constants – and their values – can be found by looking in the Microsoft Word SDK and, in particular, by looking at the wdFieldType enumeration.

After defining the constant we next create an instance of the Word.Application object and set the Visible property to True; that gives us a running instance of Word that we can see onscreen. We then use this line of code to add a new, blank document to our instance of Word:

Set objDoc = objWord.Documents.Add()

Now that we have a document we need to insert the field for the file name in the footer. To do that, we first create a Range object that encompasses the first footer in the first section of the document (which, for a brand-new document, will also be the only footer in the document):

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

And then we’re ready to insert the field:

Set objField = objDoc.Fields.Add(objRange, wdFieldFileName)

That’s right: just one line of code. We reference the Fields collection and then call the Add method. This method requires two parameters: the spot in the document where we want to insert the field (objRange, the object reference to the footer) and the value of the field to be inserted. This, of course, is where we use our constant wdFieldFileName.

Note. By default, this script left-aligns the footer. If you’d prefer the footer to be center-aligned then just add this bit of code after inserting the field: objRange.ParagraphFormat.Alignment = 1.

Of course, having a footer that displays the file name is somewhat meaningless if you don’t actually have a file name. Therefore, we added this line of code to save the file as C:\Scripts\Test.doc:

objDoc.SaveAs “C:\Scripts\Test.doc”

And that, as they say, is all, folks.

You know, it’s funny: usually when we mention a news item in this column we do so only so we can poke fun at that item. In this case, though, we happen to agree with the news: Seattle drivers are pretty polite. But, then again, it’s hard to be a rude and aggressive driver when you’re gridlocked over 8 lanes of traffic, none of which ever seem to move. You can still be rude and aggressive, mind you, but it’s awfully hard to call you a driver when you aren’t actually going anywhere.

0 comments

Discussion is closed.

Feedback usabilla icon