Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Opening a Text File and Template from the Command Line.

Opening a Text File and Template from the Command Line

Written by Allen Wyatt (last updated August 27, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016


1

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.dotm"

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 because it knows that template files don't go with text files. Instead, Word opens the file with the Normal template. 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 the Normal template 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 template as something else (such as OldNormal.dotm) and then rename the template you want to use as Normal.dotm. When you open the text file from the command line, the desired set of styles will be in the new Normal.dotm 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.dotm"
        .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.dotm (where the TextTemplate macro is stored) is attached to the text file, and then the macro attaches the other template automatically.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11187) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Opening a Text File and Template from the Command Line.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adjusting the Angle of Axis Labels

When creating a chart, your axis labels may be wider than desired. One way to deal with them is to change the angle at ...

Discover More

Specifying a Location To Save Automatic Backup Files

Word allows you make two types of automatic backups of your documents. One is a copy of the file before you start making ...

Discover More

Merging and Printing

When you merge information into a document, Word provides two different ways you can create your output. Here's an ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Converting Many DOC Files to DOCX

Need to convert a bunch of old DOC files to the newer DOCX format? Word doesn't provide the capability to convert a bunch ...

Discover More

Formatting Text in Custom Document Properties

Word allows you to create custom document properties that stay with a document and can be inserted through the use of ...

Discover More

Leading Spaces in Document File Names

If you try to add spaces to the beginning of a document's file name, Word normally strips them away. This tip examines ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2017-08-07 11:11:33

Drew Trott

You have a typo in the last line of code; should end with /m"TextTempate" (with an "l").


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.