Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. 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

by Allen Wyatt
(last updated October 12, 2016)

5

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 of and the ".docx" extension added.

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 and 2010. 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

Using Object Anchors

An object anchor is used to signify the point at which an object is inserted into a document. If you want to see these ...

Discover More

Easily Adding Blank Rows

Want to add a bunch of blank rows to a your data and have those rows interspersed among your existing rows? Here's a quick ...

Discover More

Correctly Saving Delimited Files

Delimited files are often created through Excel so that your data can be exported to other programs. If the delimited data ...

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)

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object model ...

Discover More

Counting Open Document Windows

When creating macros, it is sometimes necessary to know how many documents are open in Word. This is relatively easy to do ...

Discover More

Aligning a Paragraph in a Macro

If you are applying formatting from within a macro, you may want to change the alignment of various paragraphs. Here's how ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 9 + 4?

2017-02-13 03:17:26

tamizharasan

Danial, i tried the same format to save the file as html and it worked. Thank you.


2014-06-09 18:39:52

pajamauniform

Daniel, try adding ActiveDocument.Convert


2013-11-25 19:05:33

Daniel

Found m y problem, the fileformat needed to be wdFormatXMLDocument


2013-11-25 18:51:43

Daniel

I tried to open the document on another computer. The system saving the document was Office 2007. When I tried to open in Office 2013 on another computer, the error message was more descriptive: it said the format of the document didn't match the extension.


2013-11-25 18:46:34

Daniel

Hi, thanks for this tip. I thought i was golden with this but when I tried to open the new document it came up as needing repair. The "Open and Repair" option does not work for the file either. Here is my macro:

Sub SaveAsDocXthenKill()
'
' SaveAsDocXthenKill Macro
'
'
Dim j As Long

j = InStrRev(ActiveDocument.FullName, ".")
ToBeKilled = ActiveDocument.FullName

sDocName = Left(ActiveDocument.FullName, j) & "docx"

ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=wdFormatDocument



ActiveDocument.Close

Kill ToBeKilled

End Sub


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.

Newest Tips
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.