Microsoft KB Archive/86714

{|
 * width="100%"|

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

 * }

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 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 &quot;c:\winword\test.doc&quot; As &quot;c:\winword\test2.doc&quot; 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  PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this &quot;as is&quot; 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 &quot;c:\winword\test.doc&quot; As &quot;c:\winword\test2.doc&quot; If err = 102 Then MsgBox &quot;File rename error occurred.&quot; If err = 53 Then MsgBox &quot;File not found.&quot; 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 &quot;Mac HD:Desktop Folder:Test Document&quot; As &quot;Mac HD:Desktop \ Folder:Regular Document&quot; 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