Written by Allen Wyatt (last updated March 2, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, and Word in Microsoft 365
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:
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, 2021, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Saving in Document Format from a Macro.
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!
One of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...
Discover MoreAn elegant way to run macros from within macros is to use the Call statement. In order to use it, you need to provide a ...
Discover MoreDo you want to easily jump to the top of a page in your document? You can use the Go To command to make the shift, or you ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments