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: Saving in a Macro Using a Desired File Name.

Saving in a Macro Using a Desired File Name

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


Steve creates documents that require the document's file name to be set forth in the header and in legal captions on the first and second pages of the document. He has created a macro that, after the file name is typed in the heading, copies it and pastes it in the text form fields in the captions at the proper places. His next document assembly step is to save the document using the "Save As" command. The document is to be saved using the same file name entered in the header and captions.

When doing this manually, Steve displays the Save As dialog box, pastes the file name into it, then clicks Save. He has tried to create the macro code to add this "Save As" step, but cannot figure out how to do it. When Word records the manual steps, Steve gets something like the following:

ActiveDocument.SaveAs FileName:="04562.sw.docx

This shows that Word is recording the specific file name being used when going through the manual steps, but Steve wants to use a different file name, based on what is in the header of the document. That information (the file name) happens to be in the Clipboard when the macro is run, so it is also possible to grab the information form the Clipboard, but Steve is also at a loss as to how to do that.

Since Steve already has a macro to copy the file name, and he is happy with that macro, it should only take a couple of changes to his code to get the file saved using the proper name. Try adding the following code to the appropriate places in your existing macro:

Dim strTemp As String
Dim MyData As DataObject
Set MyData = New DataObject

MyData.GetFromClipboard
strTemp = MyData.GetText(1)
ActiveDocument.SaveAs FileName:=Trim(strTemp) & ".docx"

As long as you have a text string in the Clipboard that you want to use for your filename, this code should work. It even adds the .Doc extension to the end of the string in the Clipboard. This code requires that you set up a reference for the Microsoft Forms in the VBA Editor. (Choose References from the Tools menu in the Editor.)

Of course, it should be pointed out that you may want to rethink your approach to this macro. In fact, you may be able to get rid of most of your code if you take a different approach entirely. For instance, you could have people first save the document and then reference the file name in the document itself through the use of fields. (The FILENAME field could be used for this purpose.)

Instead of using a macro to copy the file name to the first and second pages of the document, you could also use a defined style (such as DocTitle or DocName) to refer to your file name, and then use the STYLEREF field elsewhere in your document to make reference to that name. That way it only has to be entered once, and no macro is necessary. You can find multiple ways of repeating data in different areas of the document at this page on a Word MVP's site:

http://gregmaxey.mvps.org/Repeating_Data.htm

You could also create a UserForm to collect the data from the user (to get the file name desired), and then save the document under that name. The macro could then assign the file name to the TITLE field, and anyplace in the document that referenced the TITLE field would automatically show the file name the user entered.

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 (10809) 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 a Macro Using a Desired File Name.

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

Right Aligning a Table Column with an Indent

Word allows you to get a bit fancy in formatting the alignment of your tables. In this tip, you discover how to enter ...

Discover More

Removing Breaks

Word allows you to add several types of "breaks" into your document. If you later want to remove any of them, you can use ...

Discover More

Adding Ampersands in Headers and Footers

Add an ampersand to the text in a header or footer and you may be surprised that the ampersand disappears on your ...

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)

Embedding TrueType Fonts by Default

If you use TrueType fonts frequently, you might want to set Word to embed those fonts by default. Here's how to do it.

Discover More

Inserting Only Part of a File

You can easily insert one document within another document. What you may not know is that you can limit which part of a ...

Discover More

Losing Information in a Network Document

Saving documents on a network drive can be convenient. It can also be frustrating if it seems like your changes aren't ...

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 one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.