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

ID: Q86714


The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Windows versions 3.0, 3.1
  • Microsoft Word for the Macintosh, versions 6.0, 6.0.1


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> 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 : MACINTOSH:6.0,6.0.1; WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a-CD,2.0b,2.0c,3.0,3.1,6.0,6.0a,6.0c,7.0,7.0a
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


Last Reviewed: December 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.