
Tips.Net > WordTips Home > Files > Opening a Text File and Template from the Command Line
Summary: Word includes a command-line syntax that you can use to open files and do other operations. If you want to load a text file from the command line and have Word use a specific template with that file, you might have a hard time getting it done. If you apply the techniques in this tip, you can get just what you want. (This tip works with Microsoft Word 97, Word 2000, Word 2002, Word 2003, and Word 2007.)
Dan wants to open a text file (txt extension) with Word, from the command line, using a specific template. Only problem is, he is having some problems doing it. Dan can put together the proper command line, according to Word's Help system, but it still doesn't work.
Actually, this makes sense that it doesn't work. Consider for a moment that templates are used with Word documents, not with text files. Text files are just that: text. Even in Word they are still text. Yes, you can attach a template to a text file opened in Word, but at that point the "attachment" is not complete unless you still save the document in Word format. So, when you use a command line such as the following, it won't work:
"c:\Program Files\Microsoft Office\Office\Winword.exe" "d:\text.txt" /t"MyTemplate.dot"
It is the /t switch that is supposed to tell Word which template to use with the file it opens. The reason is that Word ignores any template specification is because it knows that template files don't go with text files. Instead, Word opens the file with the standard template, Normal.dot. Word also ignores the template switch even if you use /z instead of /t. (The /z switch is the same as /t, except that it opens Word in a slightly different manner than with /t.)
The fact that Normal.dot is used for text files provides one way that you can work around Word's apparent shortcoming in this area. You could, for instance, rename your existing Normal.dot template as something else (such as OldNormal.dot) and then rename the template you want to use as Normal.dot. When you open the text file from the command line, the desired set of styles will be in the new Normal.dot file and attached to the text file.
Another option that is perhaps better is to use a macro to attach the desired template to your text file. Yes, you could create a macro that you manually run after loading the text file, but a better way is to set things up so that it happens automatically. For instance, you might have a macro such as this in your Normal.dot template:
Sub TextTemplate()
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "d:\test files\MyTemplate.dot"
.XMLSchemaReferences.AutomaticValidation = True
.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
End With
End Sub
While you could run this macro manually to attach the MyTemplate.dot template to your document, you could also use the following from the command line:
"c:\Program Files\Microsoft Office\Office\Winword.exe" "d:\text.txt" /m"TextTempate"
In this case, the text file is opened and the TextTemplate macro is run. This works because Normal.dot (where TextTemplate is stored) is attached to the text file, and then the macro attaches the other template automatically.
Tip #455 applies to Microsoft Word versions: 97 2000 2002 2003 2007
Great Idea! Uncover the many ways you can master the full potential of printing your documents. WordTips: Printing and Printers can help you get the most from both the printed page and your printer.
Have thousands of WordTips at your fingertips, on your own system. Answer your own questions or help support others. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Pet Tips
Word2007 Tips
WordTips