Hey, Scripting Guy! In Microsoft Word, how can I change the file locations for User Templates and Workgroup Templates?
— SB
Hey, SB. At least one of the Scripting Guys has always had a lifelong fear that he would write, say, the greatest novel ever written, except that no one would know about it or care about it. Well, not until he died, of course. At that point, the book would be discovered, it would rocket to the top of the best-seller list, and it would be the Scripting Heir who would become rich.
Note. To guard against this happening, this Scripting Guy has vowed never to write anything good. So far he’s managed to stick to that promise without any problem. |
This same Scripting Guy has a similar fear regarding scripts for managing Microsoft Office. The Script Center actually has a number of scripts available in the Scripting for Office center that show you how to carry out administrative tasks such as changing file locations. Few people seem to use these, yet tons of people ask how they can write scripts for managing Microsoft Office. It’s like that great, undiscovered novel in the attic: if something were to happen to this Scripting Guy, the Scripting Heir would inherit all those scripts. Heaven forbid!
That’s why we decided to do this brief infomercial before answering our question. If you’re looking for scripts that carry out administrative tasks for Microsoft Office be sure to check out the Scripting for Office center. And if you see some 15-year-old kid nosing around there, remind him that his Scripting Dad is in tip-top shape and will probably live for another 100 years. Or more.
As for changing the file locations for User Templates and Workgroup Templates, well, this script ought to do the trick:
Const wdUserTemplatesPath = 2 Const wdWorkgroupTemplatesPath = 3Set objWord = CreateObject(“Word.Application”) Set objOptions = objWord.Options
objOptions.DefaultFilePath(wdUserTemplatesPath) = “C:\Templates” objOptions.DefaultFilePath(wdWorkgroupTemplatesPath) = “C:\Workgroup\Templates”
objWord.Quit
We begin by defining a pair of constants – wdUserTemplatesPath and wdWorkgroupTemplatesPath – that we’ll use to indicate the file locations we want to change. There are a number of other file locations that can be changed using a script; for a complete list (and for the constants and their associated values) see the wdDefaultFilePath enumeration in the Microsoft Word VBA Language Reference on MSDN.
We then create an instance of the Word.Application object as well as its child object, Options. You might notice in this script that we don’t set the Visible property to True like we do in most of our sample scripts. That’s because we don’t want Word to be visible onscreen; we want to open up Word in an invisible window, make our changes, and then close Word without anyone seeing a thing.
To change a file path all we need to do is change the value of the DefaultFilePath property, making sure to use one of our constants to indicate which file path we want to change. For example, to change the User Templates path to C:\Templates we use this line of code:
objOptions.DefaultFilePath(wdUserTemplatesPath) = “C:\Templates”
And to change the Workgroup Templates path to C:\Workgroup\Templates we use this line of code:
objOptions.DefaultFilePath(wdWorkgroupTemplatesPath) = “C:\Workgroup\Templates”
That’s all we have to do; we don’t even need to call a Save function of any kind. We change the file locations and then use the Quit method to close Word. (If we don’t do that, an invisible instance of Word will continue running in the background.)
And now that people know about the scripts for managing Microsoft Office, we do one other thing: sit back and wait for fame and fortune to roll in.
0 comments