Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. 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: Saving in Document Format from a Macro.

Saving in Document Format from a Macro

Written by Allen Wyatt (last updated March 2, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


1

Anthony regularly imports multiple reports, in text files (i.e, filename.txt), into Word. He then uses macros to format this imported information. After the formatting macro is complete, Anthony manually uses "Save As" for each report to save it as a Word document. He is wondering what commands he should add to his macro to automate the last step of saving the data. Anthony would like to have the macro save the file using the same root name as the original text file, only changing it to Word format, as in filename.docx.

The steps to actually save the file are relatively easy. Consider the following code snippet, which saves a document in Word format:

ActiveDocument.SaveAs FileName:=sDocName, _
  FileFormat:=wdFormatDocumentDefault

The name of the file is stored in the sDocName variable, and the setting for the FileFormat property indicates that you want the document saved in the Word format. In an existing macro, the only thing left to do would be to set up sDocName with the filename that is desired.

Assuming that you have saved your original text file name into the variable sOrigName, you could use the following code to change the extension to .doc, and then save the file:

sDocName = Left(sOrigName, Len(sOrigName) - 4)
sDocName = sDocName & ".docx"
ActiveDocument.SaveAs FileName:=sDocName, _
  FileFormat:=wdFormatDocumentDefault

The code assumes that the last four characters of sOrigName contain the filename extension (the period plus three characters). These are stripped off and the ".docx" extension added. If you aren't sure how long the filename extension will be for the original file, you can rely on the Split function to pull it apart, if desired:

sNameParts = Split(sOrigName, ".")
sDocName = sNameParts(0) & ".docx"
ActiveDocument.SaveAs FileName:=sDocName, _
  FileFormat:=wdFormatDocumentDefault

To make this macro work, make sure you declare sNameParts as a string array.

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 (404) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Saving in Document Format from a Macro.

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

Printing an Entire Workbook by Default

Need to print an entire workbook? It's as easy as adding a single line of code to your macros.

Discover More

Complex Compound Formatting

Sometimes it can seem that the formatting needs of your document can easily outstrip the capabilities of Word. This is ...

Discover More

Changing Your System Date and Time

Need to change the date and time maintained by Windows? It's easy to do using the steps in this tip.

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Inserting the Time Remaining Until a Target Date and Time

Would you like a countdown value of some type to appear in your document? You can create your own through the use of a ...

Discover More

Swapping Two Strings

Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...

Discover More

Aligning Paragraphs in a Macro

Using a macro to format your document (or portions of your document) is not all that uncommon. If you want your macro to ...

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?

2024-03-03 16:05:31

William

A more reliable way to derive the sDocName value — if you aren't sure how long the original filename extension will be OR if there will be only one period in the filename — would be to use something like the following:

sDocName = Left(sOrigName, InStrRev(sOrigName, ".") - 1) & ".docx"

For example, this will derive:

filename.docx from filename.text
and
filename.001.docx from filename.001.txt


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.