Written by Allen Wyatt (last updated September 16, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016
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:=wdFormatDocument
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:=wdFormatDocument
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:=wdFormatDocument
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, and 2016. You can find a version of this tip for the older menu interface of Word here: Saving in Document Format from a Macro.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's ...
Discover MoreThe main body of your text is only one part of what makes up the entire document. Documents can consist of other ...
Discover MoreMacros can make life easier, as they provide a fast and efficient way of processing text in a document. Such is the case ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-06-11 09:09:17
Vitaly
According to MSDN, you should use 'wdFormatDocumentDefault' instead of 'wdFormatDocument' when saving as .DOCX.
2017-11-25 09:37:57
V.S.Rawat
when I tried to do this by recording a macro,
I find that when I click file - saveAs,
it asked me to select folder, and when I selected current folder (named E:\work), it hardcoded the name there in the macro.
that is wrong method of microsoft.
There should be some method to save the file in the "current folder" with changed extension.
hardcoding anything kills the entire purpose.
if anyone knows the vba code to select "current folder" please share.
thanks.
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 © 2023 Sharon Parq Associates, Inc.
Comments