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

Last reviewed: March 12, 1998
Article 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 operating system versions 3.0 and 3.1
  • 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 : macword winword winword2 word6 word7 word95 kbmacro
Version : WINDOWS: 1.x, 2.x, 6.0, 7.0; MACINTOSH: 6.0, 6.0.1
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 12, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.