WD: Using the WordBasic Name...As Command to Rename Files

ID: Q86714

The information in this article applies to:

SUMMARY

In Microsoft Word, the WordBasic Name...As command allows a specified file to be renamed to another file name. The syntax and function of this command is similar to the MS-DOS RENAME command.

Syntax:

  Name OldName$ As NewName$

The following macro renames the TEST.DOC file to TEST2.DOC:

  Name "c:\winword\test.doc" As "c:\winword\test2.doc"

If you do not include paths with the OldName$ and NewName$ arguments, Name...As assumes the current directory (the directory selected in the Open dialog box on File menu). By including a different path for NewName$, you can move a file to a different directory. You cannot use Name to move directories or to move a file to a different drive.

MORE INFORMATION

WARNING: ANY USE BY YOU OF THE <CODE OR MACRO CODE><![CDATA[ PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this <code or macro code> "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

If a file of the same name already exists in the target directory, Word 6.x for Windows will post WordBasic error number 102 (Command Failed). The filename you choose to rename the file to, must not already exist in the directory.

The following Word 6.x example macro demonstrates how to trap for error numbers 102 (Command Failed) and 53 (File not Found).

   Sub MAIN
      On Error Resume Next
      Name "c:\winword\test.doc" As "c:\winword\test2.doc"
      If err = 102 Then MsgBox "File rename error occurred."
      If err = 53 Then MsgBox "File not found."
   End Sub

NOTE: Word 6.x for the Macintosh users, be sure to use Mac path information on the Name...As line. For example:

   Name "Mac HD:Desktop Folder:Test Document" As "Mac HD:Desktop \ 
         Folder:Regular Document"

The WordBasic CopyFile statement allows you to copy a file to a specified directory. Like the MS-DOS COPY command, CopyFile allows you to specify a new name when copying the file.

Syntax

 CopyFile .FileName = text, .Directory = text

REFERENCES

"Using WordBasic," by WexTech Systems and Microsoft, page 258

Additional query words: Name

Keywords          : kbmacro winword macword word6 winword2 word7 word95 
Version           : WINDOWS: 1.x, 2.x, 6.0, 7.0; MACINTOSH: 6.0, 6.0.1
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: February 12, 1999