Word.Tips.Net WordTips (Menu Interface)

Saving in a Macro Using a Desired File Name

Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Saving in a Macro Using a Desired File Name.

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.doc

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

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

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:


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.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (453) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Saving in a Macro Using a Desired File Name.

Related Tips:

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!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

User    02 Jul 2015, 13:09
I need a macro that does the following:
1. Accepts all changes and turns off tracking
2. Deletes all comments
3. Saves the file with the same name but replaces "Revisions Guide" with "REVISED" at the end of the document title.

If the document is called "Document(1) Revisions Guide" then I need to rename it "Document(1) REVISED".

I don't have a problem recording the first two steps, but I don't know how to make the save as function dynamic.
Jay    04 Jun 2014, 14:15
Hi, I really appreciate the help.

My macro separates large word documents into several smaller docs using a delimiter. I want the docs to save automatically with the first sentence, which is what word prepopulates when saving a doc.

Any idea on how to do this?

MBal    30 Oct 2012, 09:52
Hi Allen,

I worked out the reference part ( making reference in VBA editor) and the solution works like a charme. However, I would very much prefer to save each file in a new directoy using the same name as the document. Do you have a sugegstion?
MBal    24 Oct 2012, 06:01
Hi Allen,

My situation exactly matches steve's issue. I have a Macro which copies the appropriate name I want to use as File name to the clipboard. When I add your code however Word returns the next error: "user- defined type not defined", highlighting MyData As DataObject.
What am I doing wrong?
Steve    25 Jan 2012, 11:44
How can I get a Macro working to use the contents of the clipboard as the name of a new file I want to save?
I can record a Macro that will select a person's name from places in the Word document and I want to use this person's name as the name of the new document and save it in a folder. While recording a Macro, I select the name to get it to the clipboard, click on "Save as," then I click on a folder, then I paste the contents of the clipboard on the "File name" then I click save.
But the resulting Maco puts the contents of the clipboard Permanantly in the File name and Will Not Allow A Variable. So if the Macro is run again, it just saves to the same place again and again.
I want to save three different files and use three different file names by selecting a whole line which will contain the peron's name or number.

• Here it is, the document had "Joan Jones" on the third line and the Macro selected that. But it put it in there permanantly. I want a variable File name.

Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    ChangeFileOpenDirectory _
        "\mokansasc2s103usersstephen.kinder000AAAAAAARemedy Tickets"
    ActiveDocument.SaveAs FileName:= _
        "\mokansasc2s103usersstephen.kinder000AAAAAAARemedy TicketsJoan Jones.docx" _
        , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.