Name Statement

Syntax

Name oldpathname As newpathname

oldpathname

Use: Required

Data Type: String

The current filename and optional path.

newpathname

Use: Required

Data Type: String

The new filename and optional path.

Description

Renames a disk file or folder.

Rules at a Glance

  • newpathname must not already exist or an error is generated.

  • newpathname and oldpathname can't be on different drives.

  • Path information included in newpathname and oldpathname can take the form of the local system's path or the UNC path.

  • newpathname and oldpathname can't include the wildcard characters ? and *.

  • You can't use the Name statement with a file that is already open.

Programming Tips and Gotchas

The Name statement can move a file from one folder to another, and optionally, can change the file's name at the same time. If the folder specified in newnamepath exists and is different from that stated in oldnamepath, the file is moved to the folder specified in newnamepath. If the filename in newnamepath is also different, the file is renamed at the same time. Moving an object in this way, however, only works with files; you can't use the Name statement to move folders.

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.